From 88a8d35c51c3fe1bb32ff2da491776abb31d0af0 Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Tue, 25 Jan 2022 15:31:09 +0200
Subject: [PATCH] Issue #3248177 by Wim Leers, marcvangend, lauriii, Lendude:
 Language toolbar item cannot be removed from the toolbar

---
 .../ckeditor5/src/Plugin/Editor/CKEditor5.php | 12 ++++++--
 .../src/FunctionalJavascript/AdminUiTest.php  | 28 +++++++++++++++++++
 .../CKEditor5AllowedTagsTest.php              | 21 --------------
 3 files changed, 38 insertions(+), 23 deletions(-)

diff --git a/core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php b/core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php
index b93b78ef0069..ed6cc6512eea 100644
--- a/core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php
+++ b/core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php
@@ -671,10 +671,18 @@ protected function getEventualEditorWithPrimedFilterFormat(SubformStateInterface
 
     $pair = static::createEphemeralPairedEditor($submitted_editor, $submitted_filter_format);
 
+    // When CKEditor 5 plugins are disabled in the form-based admin UI, the
+    // associated settings (if any) should be omitted too.
+    $original_settings = $pair->getSettings();
+    $enabled_plugins = $this->ckeditor5PluginManager->getEnabledDefinitions($pair);
+    $updated_settings = [
+      'plugins' => array_intersect_key($original_settings['plugins'], $enabled_plugins),
+    ] + $original_settings;
+    $pair->setSettings($updated_settings);
+
     if ($pair->getFilterFormat()->filters('filter_html')->status) {
       // Compute elements provided by the current CKEditor 5 settings.
-      $enabled_plugins = array_keys($this->ckeditor5PluginManager->getEnabledDefinitions($pair));
-      $elements = $this->ckeditor5PluginManager->getProvidedElements($enabled_plugins, $pair);
+      $elements = $this->ckeditor5PluginManager->getProvidedElements(array_keys($enabled_plugins), $pair);
 
       // Compute eventual filter_html setting. Eventual as in: this is the list
       // of eventually allowed HTML tags.
diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
index d2c01635e6b9..9f655886cde3 100644
--- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/AdminUiTest.php
@@ -170,4 +170,32 @@ public function testMessagesDoNotAccumulate(): void {
     $this->assertCount(1, $find_validation_error_messages());
   }
 
+  /**
+   * Tests the language config form.
+   */
+  public function testLanguageConfigForm() {
+    $page = $this->getSession()->getPage();
+    $assert_session = $this->assertSession();
+
+    $this->createNewTextFormat($page, $assert_session);
+    $assert_session->assertWaitOnAjaxRequest();
+
+    // The language plugin config form should not be present.
+    $assert_session->elementNotExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]');
+
+    $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-item-textPartLanguage'));
+    $this->triggerKeyUp('.ckeditor5-toolbar-item-textPartLanguage', 'ArrowDown');
+    $assert_session->assertWaitOnAjaxRequest();
+
+    // The language plugin config form should now be present.
+    $assert_session->elementExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]');
+
+    // It must also be possible to remove the language plugin again.
+    $this->triggerKeyUp('.ckeditor5-toolbar-item-textPartLanguage', 'ArrowUp');
+    $assert_session->assertWaitOnAjaxRequest();
+
+    // The language plugin config form should not be present anymore.
+    $assert_session->elementNotExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]');
+  }
+
 }
diff --git a/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php b/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php
index 5c06e07a853d..875c755c62c1 100644
--- a/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php
+++ b/core/modules/ckeditor5/tests/src/FunctionalJavascript/CKEditor5AllowedTagsTest.php
@@ -183,27 +183,6 @@ public function testSwitchToVersion5() {
     $assert_session->pageTextContains('The text format ckeditor has been updated');
   }
 
-  /**
-   * Tests the language config form.
-   */
-  public function testLanguageConfigForm() {
-    $page = $this->getSession()->getPage();
-    $assert_session = $this->assertSession();
-
-    $this->createNewTextFormat($page, $assert_session);
-    $assert_session->assertWaitOnAjaxRequest();
-
-    // The language plugin config form should not be present.
-    $assert_session->elementNotExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]');
-
-    $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-item-textPartLanguage'));
-    $this->triggerKeyUp('.ckeditor5-toolbar-item-textPartLanguage', 'ArrowDown');
-    $assert_session->assertWaitOnAjaxRequest();
-
-    // The language plugin config form should now be present.
-    $assert_session->elementExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]');
-  }
-
   /**
    * Tests that the img tag is added after enabling image uploads.
    */
-- 
GitLab