core/modules/ckeditor/ckeditor.module | 11 +-- .../modules/ckeditor/src/CKEditorPluginManager.php | 30 +++++-- .../tests/src/Unit/CKEditorPluginManagerTest.php | 97 ++++++++++++++++++++++ 3 files changed, 124 insertions(+), 14 deletions(-) diff --git a/core/modules/ckeditor/ckeditor.module b/core/modules/ckeditor/ckeditor.module index d39d95a..b7ba596 100644 --- a/core/modules/ckeditor/ckeditor.module +++ b/core/modules/ckeditor/ckeditor.module @@ -70,11 +70,12 @@ function ckeditor_ckeditor_css_alter(array &$css, Editor $editor) { $css[] = drupal_get_path('module', 'ckeditor') . '/css/plugins/drupalimagecaption/ckeditor.drupalimagecaption.css'; } - // @todo: This should only be loaded when the Language button is enabled; - // the CKEditor text editor plugin should load this automatically, - // CKEditor plugins that want to load additional CSS *IN* CKEditor - // shouldn't have to implement hook_ckeditor_css_alter(). - $css[] = drupal_get_path('module', 'ckeditor') . '/css/plugins/language/ckeditor.language.css'; + // @todo: Remove in https://www.drupal.org/node/2645100. + /** @var \Drupal\ckeditor\CKEditorPluginManager $ckeditor_plugin_manager */ + $ckeditor_plugin_manager = \Drupal::service('plugin.manager.ckeditor.plugin'); + if (in_array('Language', $ckeditor_plugin_manager->getEnabledButtons($editor))) { + $css[] = drupal_get_path('module', 'ckeditor') . '/css/plugins/language/ckeditor.language.css'; + } } /** diff --git a/core/modules/ckeditor/src/CKEditorPluginManager.php b/core/modules/ckeditor/src/CKEditorPluginManager.php index 07d811f..cd0a1bf 100644 --- a/core/modules/ckeditor/src/CKEditorPluginManager.php +++ b/core/modules/ckeditor/src/CKEditorPluginManager.php @@ -71,15 +71,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac */ public function getEnabledPluginFiles(Editor $editor, $include_internal_plugins = FALSE) { $plugins = array_keys($this->getDefinitions()); - // Flatten each row. - $toolbar_rows = array(); - $settings = $editor->getSettings(); - foreach ($settings['toolbar']['rows'] as $row_number => $row) { - $toolbar_rows[] = array_reduce($settings['toolbar']['rows'][$row_number], function (&$result, $button_group) { - return array_merge($result, $button_group['items']); - }, array()); - } - $toolbar_buttons = array_unique(NestedArray::mergeDeepArray($toolbar_rows)); + $toolbar_buttons = $this->getEnabledButtons($editor); $enabled_plugins = array(); $additional_plugins = array(); @@ -121,6 +113,26 @@ public function getEnabledPluginFiles(Editor $editor, $include_internal_plugins } /** + * Gets the enabled toolbar buttons in the given text editor instance. + * + * @param \Drupal\editor\Entity\Editor $editor + * A configured text editor object. + * + * @return string[] + * A list of the toolbar buttons enabled in the given text editor instance. + */ + public static function getEnabledButtons(Editor $editor) { + $toolbar_rows = []; + $settings = $editor->getSettings(); + foreach ($settings['toolbar']['rows'] as $row_number => $row) { + $toolbar_rows[] = array_reduce($settings['toolbar']['rows'][$row_number], function (&$result, $button_group) { + return array_merge($result, $button_group['items']); + }, []); + } + return array_unique(NestedArray::mergeDeepArray($toolbar_rows)); + } + + /** * Retrieves all available CKEditor buttons, keyed by plugin ID. * * @return array diff --git a/core/modules/ckeditor/tests/src/Unit/CKEditorPluginManagerTest.php b/core/modules/ckeditor/tests/src/Unit/CKEditorPluginManagerTest.php index e69de29..4184347 100644 --- a/core/modules/ckeditor/tests/src/Unit/CKEditorPluginManagerTest.php +++ b/core/modules/ckeditor/tests/src/Unit/CKEditorPluginManagerTest.php @@ -0,0 +1,97 @@ + [ + [], + [] + ], + '1 row, 1 group' => [ + [ + // Row 1. + [ + // Group 1. + ['name' => 'Formatting', 'items' => ['Bold', 'Italic']], + ] + ], + ['Bold', 'Italic'] + ], + '1 row, >1 groups' => [ + [ + // Row 1. + [ + // Group 1. + ['name' => 'Formatting', 'items' => ['Bold', 'Italic']], + // Group 2. + ['name' => 'Linking', 'items' => ['Link']], + ], + ], + ['Bold', 'Italic', 'Link'] + ], + '2 rows, 1 group each' => [ + [ + // Row 1. + [ + // Group 1. + ['name' => 'Formatting', 'items' => ['Bold', 'Italic']], + ], + // Row 2. + [ + // Group 1. + ['name' => 'Tools', 'items' => ['Source']], + ], + ], + ['Bold', 'Italic', 'Source'], + ], + '2 rows, >1 groups each' => [ + [ + // Row 1. + [ + // Group 1. + ['name' => 'Formatting', 'items' => ['Bold', 'Italic']], + // Group 2. + ['name' => 'Linking', 'items' => ['Link']], + ], + // Row 2. + [ + // Group 1. + ['name' => 'Tools', 'items' => ['Source']], + // Group 2. + ['name' => 'Advanced', 'items' => ['Llama']], + ], + ], + ['Bold', 'Italic', 'Link', 'Source', 'Llama'] + ], + ]; + } + + /** + * @covers ::getEnabledButtons + * @dataProvider providerGetEnabledButtons + */ + public function testGetEnabledButtons(array $toolbar_rows, array $expected_buttons) { + $editor= $this->prophesize(Editor::class); + $editor->getSettings() + ->willReturn(['toolbar' => ['rows' => $toolbar_rows]]); + + $enabled_buttons = CKEditorPluginManager::getEnabledButtons($editor->reveal()); + $this->assertEquals($expected_buttons, $enabled_buttons); + } + +}