video_embed_field-8.x-2.4/modules/video_embed_wysiwyg/video_embed_wysiwyg.module
modules/video_embed_wysiwyg/video_embed_wysiwyg.module
<?php
/**
* @file
* Module file for video_embed_wysiwyg.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\video_embed_wysiwyg\VideoEmbedWysiwygConstants;
use Drupal\Component\Utility\UrlHelper;
/**
* Implements hook_form_FORM_ID_alter().
*/
function video_embed_wysiwyg_form_filter_format_form_alter(&$form, $form_state, $form_id) {
$form['#validate'][] = 'video_embed_wysiwyg_filter_weight_validate';
}
/**
* Validate the filters are not in an order that will cause conflicts.
*/
function video_embed_wysiwyg_filter_weight_validate($form, FormStateInterface $form_state) {
// Don't validate if the WYSIWYG filter is not enabled.
if (empty($form_state->getValue(['filters', 'video_embed_wysiwyg', 'status']))) {
return;
}
$wysiwyg_weight = $form_state->getValue(['filters', 'video_embed_wysiwyg', 'weight']);
// Check the WYSIWYG filter runs before url filtering.
if (!empty($form_state->getValue(['filters', 'filter_url', 'status']))) {
$filter_weight = $form_state->getValue(['filters', 'filter_url', 'weight']);
if ($wysiwyg_weight > $filter_weight) {
$form_state->setError($form['filters']['status']['video_embed_wysiwyg'], t('The "Video Embed WYSIWYG" filter must run before the "Convert URLs into links" filter to function correctly. For more information <a href="@url">read the documentation</a>.', ['@url' => VideoEmbedWysiwygConstants::VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL]));
}
}
// Check the WYSIWYG filter runs after the HTML tag filter.
if (!empty($form_state->getValue(['filters', 'filter_html', 'status']))) {
$html_filter_weight = $form_state->getValue(['filters', 'filter_html', 'weight']);
if ($wysiwyg_weight < $html_filter_weight) {
$form_state->setError($form['filters']['status']['video_embed_wysiwyg'], t('The "Video Embed WYSIWYG" filter must run after the "Limit allowed HTML tags" filter to function correctly. For more information <a href="@url">read the documentation</a>.', ['@url' => VideoEmbedWysiwygConstants::VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL]));
}
}
}
/**
* Directly taken from https://www.drupal.org/docs/core-modules-and-themes/core-modules/ckeditor-5-module/how-to-style-custom-content-in-ckeditor-5#s-registering-ckeditor-5-stylesheets-from-a-module.
*
* Implements hook_library_info_alter().
*/
function video_embed_wysiwyg_library_info_alter(&$libraries, $extension) {
$module = 'video_embed_wysiwyg';
if ($extension === 'ckeditor5') {
// Add paths to stylesheets specified by a modules's ckeditor5-stylesheets
// config property.
$module_path = \Drupal::service('extension.list.module')->getPath($module);
$info = \Drupal::service('extension.list.module')->getExtensionInfo($module);
if (isset($info['ckeditor5-stylesheets']) && $info['ckeditor5-stylesheets'] !== FALSE) {
$css = $info['ckeditor5-stylesheets'];
foreach ($css as $key => $url) {
// CSS URL is external or relative to Drupal root.
if (UrlHelper::isExternal($url) || $url[0] === '/') {
$css[$key] = $url;
}
// CSS URL is relative to theme.
else {
$css[$key] = '/' . $module_path . '/' . $url;
}
}
}
if (!isset($libraries['internal.drupal.ckeditor5.stylesheets']['css']['theme'])) {
$libraries['internal.drupal.ckeditor5.stylesheets'] = [
'css' => [
'theme' => array_fill_keys(array_values($css), []),
],
];
}
else {
foreach (array_values($css) as $value) {
$libraries['internal.drupal.ckeditor5.stylesheets']['css']['theme'][$value] = [];
}
}
}
}
