Issue #3271057 by Wim Leers, xjm, lauriii: Move Media Library CKEditor 4 integrations from Media into CKEditor

merge-requests/2612/head
catch 2022-06-28 18:47:22 +01:00
parent 3a66136c40
commit 13051c34e3
9 changed files with 82 additions and 82 deletions

View File

@ -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);
}
}
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\media_library\Plugin\CKEditorPlugin;
namespace Drupal\ckeditor\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@ -18,6 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @CKEditorPlugin(
* id = "drupalmedialibrary",
* label = @Translation("Embed media from the Media Library"),
* provider = "media_library",
* )
*
* @internal
@ -101,7 +102,7 @@ class DrupalMediaLibrary extends CKEditorPluginBase implements ContainerFactoryP
* {@inheritdoc}
*/
public function getFile() {
return $this->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',
],
];
}

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
namespace Drupal\Tests\ckeditor\FunctionalJavascript;
use Drupal\Component\Utility\Html;
use Drupal\editor\Entity\Editor;
@ -14,10 +14,10 @@ use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
/**
* @coversDefaultClass \Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary
* @group media_library
* @coversDefaultClass \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalMediaLibrary
* @group ckeditor
*/
class CKEditorIntegrationTest extends WebDriverTestBase {
class MediaLibraryTest extends WebDriverTestBase {
use CKEditorTestTrait;
use CKEditorAdminSortTrait;

View File

@ -26,8 +26,6 @@ use Drupal\media_library\Form\OEmbedForm;
use Drupal\media_library\MediaLibraryState;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Component\Serialization\Json;
/**
* Implements hook_help().
@ -474,73 +472,3 @@ function _media_library_configure_view_display(MediaTypeInterface $type) {
]);
return (bool) $display->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);
}
}
}
}

View File

@ -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 {