View source
<?php
use Drupal\editor\Entity\Editor;
use Drupal\Core\Form\FormStateInterface;
define('VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL', 'https://www.drupal.org/node/2805545');
function video_embed_wysiwyg_ckeditor_css_alter(array &$css, Editor $editor) {
$css[] = drupal_get_path('module', 'video_embed_wysiwyg') . '/plugin/plugin.css';
}
function video_embed_wysiwyg_form_filter_format_form_alter(&$form, $form_state, $form_id) {
$form['#validate'][] = 'video_embed_wysiwyg_toolbar_filter_validate';
$form['#validate'][] = 'video_embed_wysiwyg_filter_weight_validate';
}
function video_embed_wysiwyg_toolbar_filter_validate($form, FormStateInterface $form_state) {
$filter_enabled = !empty($form_state
->getValue([
'filters',
'video_embed_wysiwyg',
'status',
]));
$button_enabled = FALSE;
$button_rows = json_decode($form_state
->getValue([
'editor',
'settings',
'toolbar',
'button_groups',
]), TRUE);
if (!empty($button_rows)) {
foreach ($button_rows as $button_row) {
foreach ($button_row as $button_group) {
foreach ($button_group['items'] as $item) {
if ($item === 'video_embed') {
$button_enabled = TRUE;
break 2;
}
}
}
}
}
if ($filter_enabled !== $button_enabled) {
$form_state
->setError($form['filters']['status']['video_embed_wysiwyg'], t('To embed videos, make sure you have enabled the "Video Embed WYSIWYG" filter and dragged the video icon into the WYSIWYG toolbar. For more information <a href="@url">read the documentation</a>.', [
'@url' => VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL,
]));
}
}
function video_embed_wysiwyg_filter_weight_validate($form, FormStateInterface $form_state) {
if (empty($form_state
->getValue([
'filters',
'video_embed_wysiwyg',
'status',
]))) {
return;
}
$wysiwyg_weight = $form_state
->getValue([
'filters',
'video_embed_wysiwyg',
'weight',
]);
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' => VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL,
]));
}
}
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' => VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL,
]));
}
}
}