Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / video_embed_field / modules / video_embed_wysiwyg / video_embed_wysiwyg.module
1 <?php
2
3 /**
4  * @file
5  * Module file for video_embed_wysiwyg.
6  */
7
8 use Drupal\editor\Entity\Editor;
9 use Drupal\Core\Form\FormStateInterface;
10
11 // The documentation URL for the WYSIWYG filter.
12 define('VIDEO_EMBED_WYSIWYG_DOCUMENTATION_URL', 'https://www.drupal.org/node/2805545');
13
14 /**
15  * Implements hook_ckeditor_css_alter().
16  */
17 function video_embed_wysiwyg_ckeditor_css_alter(array &$css, Editor $editor) {
18   $css[] = drupal_get_path('module', 'video_embed_wysiwyg') . '/plugin/plugin.css';
19 }
20
21 /**
22  * Implements hook_form_FORM_ID_alter().
23  */
24 function video_embed_wysiwyg_form_filter_format_form_alter(&$form, $form_state, $form_id) {
25   $form['#validate'][] = 'video_embed_wysiwyg_toolbar_filter_validate';
26   $form['#validate'][] = 'video_embed_wysiwyg_filter_weight_validate';
27 }
28
29 /**
30  * Validate callback to check if the filter and button are both enabled.
31  */
32 function video_embed_wysiwyg_toolbar_filter_validate($form, FormStateInterface $form_state) {
33   $filter_enabled = !empty($form_state->getValue(['filters', 'video_embed_wysiwyg', 'status']));
34   $button_enabled = FALSE;
35   $button_rows = json_decode($form_state->getValue(['editor', 'settings', 'toolbar', 'button_groups']), TRUE);
36   if (!empty($button_rows)) {
37     foreach ($button_rows as $button_row) {
38       foreach ($button_row as $button_group) {
39         foreach ($button_group['items'] as $item) {
40           if ($item === 'video_embed') {
41             $button_enabled = TRUE;
42             break 2;
43           }
44         }
45       }
46     }
47   }
48   // The button and filter can either both be enabled or disabled.
49   if ($filter_enabled !== $button_enabled) {
50     $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]));
51   }
52 }
53
54 /**
55  * Validate the filters are not in an order that will cause conflicts.
56  */
57 function video_embed_wysiwyg_filter_weight_validate($form, FormStateInterface $form_state) {
58   // Don't validate if the WYSIWYG filter is not enabled.
59   if (empty($form_state->getValue(['filters', 'video_embed_wysiwyg', 'status']))) {
60     return;
61   }
62
63   $wysiwyg_weight = $form_state->getValue(['filters', 'video_embed_wysiwyg', 'weight']);
64
65   // Check the WYSIWYG filter runs before url filtering.
66   if (!empty($form_state->getValue(['filters', 'filter_url', 'status']))) {
67     $filter_weight = $form_state->getValue(['filters', 'filter_url', 'weight']);
68     if ($wysiwyg_weight > $filter_weight) {
69       $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]));
70     }
71   }
72   // Check the WYSIWYG filter runs after the HTML tag filter.
73   if (!empty($form_state->getValue(['filters', 'filter_html', 'status']))) {
74     $html_filter_weight = $form_state->getValue(['filters', 'filter_html', 'weight']);
75     if ($wysiwyg_weight < $html_filter_weight) {
76       $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]));
77     }
78   }
79 }