diff --git a/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php b/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php index 44aadf54087..41730e80ddd 100644 --- a/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php +++ b/core/modules/media_library/src/Plugin/CKEditorPlugin/DrupalMediaLibrary.php @@ -108,6 +108,14 @@ class DrupalMediaLibrary extends CKEditorPluginBase implements ContainerFactoryP * {@inheritdoc} */ public function getConfig(Editor $editor) { + // If the editor has not been saved yet, we may not be able to create a + // coherent MediaLibraryState object, which is needed in order to generate + // the required configuration. But, if we're creating a new editor, we don't + // need to do that anyway, so just return an empty array. + if ($editor->isNew()) { + return []; + } + $media_type_ids = $this->mediaTypeStorage->getQuery()->execute(); if (in_array('image', $media_type_ids, TRUE)) { diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php index 3daea82dcd3..40ec51d863b 100644 --- a/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php +++ b/core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php @@ -122,8 +122,7 @@ class CKEditorIntegrationTest extends WebDriverTestBase { } /** - * Tests that media_embed filter is required to enable the DrupalMediaLibrary - * button. + * Tests validation that DrupalMediaLibrary requires media_embed filter. */ public function testConfigurationValidation() { $page = $this->getSession()->getPage(); @@ -141,6 +140,33 @@ class CKEditorIntegrationTest extends WebDriverTestBase { $page->checkField('filters[media_embed][status]'); $page->pressButton('Save configuration'); $assert_session->pageTextContains('The text format Test format has been updated.'); + + // Now test adding a new format. + $this->drupalGet('/admin/config/content/formats/add'); + $page->fillField('name', 'Sulaco'); + // Wait for machine name to be filled in. + $this->assertNotEmpty($assert_session->waitForText('sulaco')); + $page->checkField('roles[authenticated]'); + $page->selectFieldOption('editor[editor]', 'ckeditor'); + $this->assertNotEmpty($target = $assert_session->waitForElementVisible('css', 'ul.ckeditor-toolbar-group-buttons')); + $this->assertNotEmpty($button = $assert_session->elementExists('css', 'li[data-drupal-ckeditor-button-name="DrupalMediaLibrary"]')); + $button->dragTo($target); + $page->pressButton('Save configuration'); + $assert_session->pageTextContains('The Embed media filter must be enabled to use the Insert from Media Library button.'); + $page->checkField('filters[media_embed][status]'); + $page->pressButton('Save configuration'); + $assert_session->pageTextContains('Added text format Sulaco.'); + + // Test that when adding the DrupalMediaLibrary button to the editor the + // correct attributes are added to the tag in the Allowed + // HTML tags. + $this->drupalGet('/admin/config/content/formats/manage/sulaco'); + $page->checkField('filters[filter_html][status]'); + $expected = 'drupal-media data-entity-type data-entity-uuid'; + $allowed_html = $assert_session->fieldExists('filters[filter_html][settings][allowed_html]')->getValue(); + $this->assertContains($expected, $allowed_html); + $page->pressButton('Save configuration'); + $assert_session->pageTextContains('The text format Sulaco has been updated.'); } /**