From 13051c34e3404e30e9f267c99e837a0285721e1a Mon Sep 17 00:00:00 2001 From: catch Date: Tue, 28 Jun 2022 18:47:22 +0100 Subject: [PATCH] Issue #3271057 by Wim Leers, xjm, lauriii: Move Media Library CKEditor 4 integrations from Media into CKEditor --- core/modules/ckeditor/ckeditor.module | 73 ++++++++++++++++++ .../icons/drupalmedialibrary.png | Bin .../icons/hidpi/drupalmedialibrary.png | Bin .../plugins/drupalmedialibrary/plugin.es6.js | 0 .../js/plugins/drupalmedialibrary/plugin.js | 0 .../CKEditorPlugin/DrupalMediaLibrary.php | 7 +- .../MediaLibraryTest.php} | 8 +- .../media_library/media_library.module | 72 ----------------- .../src/MediaLibraryEditorOpener.php | 4 +- 9 files changed, 82 insertions(+), 82 deletions(-) rename core/modules/{media_library => ckeditor}/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png (100%) rename core/modules/{media_library => ckeditor}/js/plugins/drupalmedialibrary/icons/hidpi/drupalmedialibrary.png (100%) rename core/modules/{media_library => ckeditor}/js/plugins/drupalmedialibrary/plugin.es6.js (100%) rename core/modules/{media_library => ckeditor}/js/plugins/drupalmedialibrary/plugin.js (100%) rename core/modules/{media_library => ckeditor}/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php (93%) rename core/modules/{media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php => ckeditor/tests/src/FunctionalJavascript/MediaLibraryTest.php} (98%) diff --git a/core/modules/ckeditor/ckeditor.module b/core/modules/ckeditor/ckeditor.module index c7fbe664e8d..5105e173236 100644 --- a/core/modules/ckeditor/ckeditor.module +++ b/core/modules/ckeditor/ckeditor.module @@ -6,8 +6,11 @@ */ use Drupal\Core\Url; +use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\UrlHelper; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\editor\Entity\Editor; /** @@ -130,3 +133,73 @@ function ckeditor_library_info_alter(&$libraries, $extension) { $libraries['drupal.ckeditor']['drupalSettings']['ckeditor']['timestamp'] = $query_string; } } + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function ckeditor_form_filter_format_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) { + // Add an additional validate callback so we can ensure the media_embed filter + // is enabled when the DrupalMediaLibrary button is enabled. + $form['#validate'][] = 'ckeditor_filter_format_edit_form_validate'; +} + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function ckeditor_form_filter_format_add_form_alter(array &$form, FormStateInterface $form_state, $form_id) { + // Add an additional validate callback so we can ensure the media_embed filter + // is enabled when the DrupalMediaLibrary button is enabled. + $form['#validate'][] = 'ckeditor_filter_format_edit_form_validate'; +} + +/** + * Validate callback to ensure the DrupalMediaLibrary button can work correctly. + */ +function ckeditor_filter_format_edit_form_validate($form, FormStateInterface $form_state) { + if ($form_state->getTriggeringElement()['#name'] !== 'op') { + return; + } + + // The "DrupalMediaLibrary" button is for the CKEditor text editor. + if ($form_state->getValue(['editor', 'editor']) !== 'ckeditor') { + return; + } + + $button_group_path = [ + 'editor', + 'settings', + 'toolbar', + 'button_groups', + ]; + + if ($button_groups = $form_state->getValue($button_group_path)) { + $buttons = []; + $button_groups = Json::decode($button_groups); + + foreach ($button_groups as $button_row) { + foreach ($button_row as $button_group) { + $buttons = array_merge($buttons, array_values($button_group['items'])); + } + } + + $get_filter_label = function ($filter_plugin_id) use ($form) { + return (string) $form['filters']['order'][$filter_plugin_id]['filter']['#markup']; + }; + + if (in_array('DrupalMediaLibrary', $buttons, TRUE)) { + $media_embed_enabled = $form_state->getValue([ + 'filters', + 'media_embed', + 'status', + ]); + + if (!$media_embed_enabled) { + $error_message = new TranslatableMarkup('The %media-embed-filter-label filter must be enabled to use the %drupal-media-library-button button.', [ + '%media-embed-filter-label' => $get_filter_label('media_embed'), + '%drupal-media-library-button' => new TranslatableMarkup('Insert from Media Library'), + ]); + $form_state->setErrorByName('filters', $error_message); + } + } + } +} diff --git a/core/modules/media_library/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png b/core/modules/ckeditor/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png similarity index 100% rename from core/modules/media_library/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png rename to core/modules/ckeditor/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png diff --git a/core/modules/media_library/js/plugins/drupalmedialibrary/icons/hidpi/drupalmedialibrary.png b/core/modules/ckeditor/js/plugins/drupalmedialibrary/icons/hidpi/drupalmedialibrary.png similarity index 100% rename from core/modules/media_library/js/plugins/drupalmedialibrary/icons/hidpi/drupalmedialibrary.png rename to core/modules/ckeditor/js/plugins/drupalmedialibrary/icons/hidpi/drupalmedialibrary.png diff --git a/core/modules/media_library/js/plugins/drupalmedialibrary/plugin.es6.js b/core/modules/ckeditor/js/plugins/drupalmedialibrary/plugin.es6.js similarity index 100% rename from core/modules/media_library/js/plugins/drupalmedialibrary/plugin.es6.js rename to core/modules/ckeditor/js/plugins/drupalmedialibrary/plugin.es6.js diff --git a/core/modules/media_library/js/plugins/drupalmedialibrary/plugin.js b/core/modules/ckeditor/js/plugins/drupalmedialibrary/plugin.js similarity index 100% rename from core/modules/media_library/js/plugins/drupalmedialibrary/plugin.js rename to core/modules/ckeditor/js/plugins/drupalmedialibrary/plugin.js diff --git a/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php similarity index 93% rename from core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php rename to core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php index b0739136d37..bfff786dbe3 100644 --- a/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php +++ b/core/modules/ckeditor/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php @@ -1,6 +1,6 @@ moduleExtensionList->getPath('media_library') . '/js/plugins/drupalmedialibrary/plugin.js'; + return $this->moduleExtensionList->getPath('ckeditor') . '/js/plugins/drupalmedialibrary/plugin.js'; } /** @@ -162,7 +163,7 @@ class DrupalMediaLibrary extends CKEditorPluginBase implements ContainerFactoryP return [ 'DrupalMediaLibrary' => [ 'label' => $this->t('Insert from Media Library'), - 'image' => $this->moduleExtensionList->getPath('media_library') . '/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png', + 'image' => $this->moduleExtensionList->getPath('ckeditor') . '/js/plugins/drupalmedialibrary/icons/drupalmedialibrary.png', ], ]; } diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php b/core/modules/ckeditor/tests/src/FunctionalJavascript/MediaLibraryTest.php similarity index 98% rename from core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php rename to core/modules/ckeditor/tests/src/FunctionalJavascript/MediaLibraryTest.php index 40fe5f7f537..ca260313b86 100644 --- a/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/MediaLibraryTest.php @@ -1,6 +1,6 @@ save(); } - -/** - * Implements hook_form_FORM_ID_alter(). - */ -function media_library_form_filter_format_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) { - // Add an additional validate callback so we can ensure the media_embed filter - // is enabled when the DrupalMediaLibrary button is enabled. - $form['#validate'][] = 'media_library_filter_format_edit_form_validate'; -} - -/** - * Implements hook_form_FORM_ID_alter(). - */ -function media_library_form_filter_format_add_form_alter(array &$form, FormStateInterface $form_state, $form_id) { - // Add an additional validate callback so we can ensure the media_embed filter - // is enabled when the DrupalMediaLibrary button is enabled. - $form['#validate'][] = 'media_library_filter_format_edit_form_validate'; -} - -/** - * Validate callback to ensure the DrupalMediaLibrary button can work correctly. - */ -function media_library_filter_format_edit_form_validate($form, FormStateInterface $form_state) { - if ($form_state->getTriggeringElement()['#name'] !== 'op') { - return; - } - - // The "DrupalMediaLibrary" button is for the CKEditor text editor. - if ($form_state->getValue(['editor', 'editor']) !== 'ckeditor') { - return; - } - - $button_group_path = [ - 'editor', - 'settings', - 'toolbar', - 'button_groups', - ]; - - if ($button_groups = $form_state->getValue($button_group_path)) { - $buttons = []; - $button_groups = Json::decode($button_groups); - - foreach ($button_groups as $button_row) { - foreach ($button_row as $button_group) { - $buttons = array_merge($buttons, array_values($button_group['items'])); - } - } - - $get_filter_label = function ($filter_plugin_id) use ($form) { - return (string) $form['filters']['order'][$filter_plugin_id]['filter']['#markup']; - }; - - if (in_array('DrupalMediaLibrary', $buttons, TRUE)) { - $media_embed_enabled = $form_state->getValue([ - 'filters', - 'media_embed', - 'status', - ]); - - if (!$media_embed_enabled) { - $error_message = new TranslatableMarkup('The %media-embed-filter-label filter must be enabled to use the %drupal-media-library-button button.', [ - '%media-embed-filter-label' => $get_filter_label('media_embed'), - '%drupal-media-library-button' => new TranslatableMarkup('Insert from Media Library'), - ]); - $form_state->setErrorByName('filters', $error_message); - } - } - } -} diff --git a/core/modules/media_library/src/MediaLibraryEditorOpener.php b/core/modules/media_library/src/MediaLibraryEditorOpener.php index 0c0af98b419..b8c07ff82f4 100644 --- a/core/modules/media_library/src/MediaLibraryEditorOpener.php +++ b/core/modules/media_library/src/MediaLibraryEditorOpener.php @@ -11,10 +11,8 @@ use Drupal\editor\Ajax\EditorDialogSave; /** * The media library opener for text editors. * - * @see \Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary - * * @internal - * This service is an internal part of Media Library's CKEditor integration. + * This is an internal part of Media Library's text editor integration. */ class MediaLibraryEditorOpener implements MediaLibraryOpenerInterface {