Commit ab2cd352 authored by webchick's avatar webchick

Issue #1905018 by Wim Leers, effulgentsia: Improve method names or return...

Issue #1905018 by Wim Leers, effulgentsia: Improve method names or return values of CKEditorPluginManager::getEnabledPlugins() and getButtonsPlugins().
parent 1e11b964
...@@ -40,7 +40,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac ...@@ -40,7 +40,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
} }
/** /**
* Determines which plug-ins are enabled. * Retrieves enabled plugins' files, keyed by plugin ID.
* *
* For CKEditor plugins that implement: * For CKEditor plugins that implement:
* - CKEditorPluginButtonsInterface, not CKEditorPluginContextualInterface, * - CKEditorPluginButtonsInterface, not CKEditorPluginContextualInterface,
...@@ -64,7 +64,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac ...@@ -64,7 +64,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
* the Drupal root-relative plugin files as values. * the Drupal root-relative plugin files as values.
* For internal plugins, the value is NULL. * For internal plugins, the value is NULL.
*/ */
public function getEnabledPlugins(Editor $editor, $include_internal_plugins = FALSE) { public function getEnabledPluginFiles(Editor $editor, $include_internal_plugins = FALSE) {
$plugins = array_keys($this->getDefinitions()); $plugins = array_keys($this->getDefinitions());
$toolbar_buttons = array_unique(NestedArray::mergeDeepArray($editor->settings['toolbar']['buttons'])); $toolbar_buttons = array_unique(NestedArray::mergeDeepArray($editor->settings['toolbar']['buttons']));
$enabled_plugins = array(); $enabled_plugins = array();
...@@ -108,16 +108,15 @@ public function getEnabledPlugins(Editor $editor, $include_internal_plugins = FA ...@@ -108,16 +108,15 @@ public function getEnabledPlugins(Editor $editor, $include_internal_plugins = FA
} }
/** /**
* Retrieves all plugins that implement CKEditorPluginButtonsInterface. * Retrieves all available CKEditor buttons, keyed by plugin ID.
* *
* @return array * @return array
* A list of the CKEditor plugins that implement buttons, with the plugin * All availble CKEditor buttons, with plugin IDs as keys and button
* IDs as keys and lists of button metadata (as implemented by getButtons()) * metadata (as implemented by getButtons()) as values.
* as values.
* *
* @see CKEditorPluginButtonsInterface::getButtons() * @see CKEditorPluginButtonsInterface::getButtons()
*/ */
public function getButtonsPlugins() { public function getButtons() {
$plugins = array_keys($this->getDefinitions()); $plugins = array_keys($this->getDefinitions());
$buttons_plugins = array(); $buttons_plugins = array();
......
...@@ -86,7 +86,7 @@ public function settingsForm(array $form, array &$form_state, EditorEntity $edit ...@@ -86,7 +86,7 @@ public function settingsForm(array $form, array &$form_state, EditorEntity $edit
$ckeditor_settings_toolbar = array( $ckeditor_settings_toolbar = array(
'#theme' => 'ckeditor_settings_toolbar', '#theme' => 'ckeditor_settings_toolbar',
'#editor' => $editor, '#editor' => $editor,
'#plugins' => $this->ckeditorPluginManager->getButtonsPlugins(), '#plugins' => $this->ckeditorPluginManager->getButtons(),
); );
$form['toolbar'] = array( $form['toolbar'] = array(
'#type' => 'container', '#type' => 'container',
...@@ -135,7 +135,7 @@ public function settingsForm(array $form, array &$form_state, EditorEntity $edit ...@@ -135,7 +135,7 @@ public function settingsForm(array $form, array &$form_state, EditorEntity $edit
} }
} }
// Get a list of all buttons that are provided by all plugins. // Get a list of all buttons that are provided by all plugins.
$all_buttons = array_reduce($this->ckeditorPluginManager->getButtonsPlugins(), function($result, $item) { $all_buttons = array_reduce($this->ckeditorPluginManager->getButtons(), function($result, $item) {
return array_merge($result, array_keys($item)); return array_merge($result, array_keys($item));
}, array()); }, array());
// Build a fake Editor object, which we'll use to generate JavaScript // Build a fake Editor object, which we'll use to generate JavaScript
...@@ -196,18 +196,18 @@ public function getJSSettings(EditorEntity $editor) { ...@@ -196,18 +196,18 @@ public function getJSSettings(EditorEntity $editor) {
$settings = array(); $settings = array();
// Get the settings for all enabled plugins, even the internal ones. // Get the settings for all enabled plugins, even the internal ones.
$enabled_plugins = array_keys($this->ckeditorPluginManager->getEnabledPlugins($editor, TRUE)); $enabled_plugins = array_keys($this->ckeditorPluginManager->getEnabledPluginFiles($editor, TRUE));
foreach ($enabled_plugins as $plugin_id) { foreach ($enabled_plugins as $plugin_id) {
$plugin = $this->ckeditorPluginManager->createInstance($plugin_id); $plugin = $this->ckeditorPluginManager->createInstance($plugin_id);
$settings += $plugin->getConfig($editor); $settings += $plugin->getConfig($editor);
} }
// Next, set the most fundamental CKEditor settings. // Next, set the most fundamental CKEditor settings.
$external_plugins = $this->ckeditorPluginManager->getEnabledPlugins($editor); $external_plugin_files = $this->ckeditorPluginManager->getEnabledPluginFiles($editor);
$settings += array( $settings += array(
'toolbar' => $this->buildToolbarJSSetting($editor), 'toolbar' => $this->buildToolbarJSSetting($editor),
'contentsCss' => $this->buildContentsCssJSSetting($editor), 'contentsCss' => $this->buildContentsCssJSSetting($editor),
'extraPlugins' => implode(',', array_keys($external_plugins)), 'extraPlugins' => implode(',', array_keys($external_plugin_files)),
// @todo: Remove image and link plugins from CKEditor build. // @todo: Remove image and link plugins from CKEditor build.
'removePlugins' => 'image,link', 'removePlugins' => 'image,link',
'language' => $language_interface->id, 'language' => $language_interface->id,
...@@ -221,7 +221,7 @@ public function getJSSettings(EditorEntity $editor) { ...@@ -221,7 +221,7 @@ public function getJSSettings(EditorEntity $editor) {
// Finally, set Drupal-specific CKEditor settings. // Finally, set Drupal-specific CKEditor settings.
$settings += array( $settings += array(
'drupalExternalPlugins' => array_map('file_create_url', $external_plugins), 'drupalExternalPlugins' => array_map('file_create_url', $external_plugin_files),
); );
ksort($settings); ksort($settings);
...@@ -238,7 +238,7 @@ public function getLibraries(EditorEntity $editor) { ...@@ -238,7 +238,7 @@ public function getLibraries(EditorEntity $editor) {
); );
// Get the required libraries for any enabled plugins. // Get the required libraries for any enabled plugins.
$enabled_plugins = array_keys($this->ckeditorPluginManager->getEnabledPlugins($editor)); $enabled_plugins = array_keys($this->ckeditorPluginManager->getEnabledPluginFiles($editor));
foreach ($enabled_plugins as $plugin_id) { foreach ($enabled_plugins as $plugin_id) {
$plugin = $this->ckeditorPluginManager->createInstance($plugin_id); $plugin = $this->ckeditorPluginManager->createInstance($plugin_id);
$additional_libraries = array_udiff($plugin->getLibraries($editor), $libraries, function($a, $b) { $additional_libraries = array_udiff($plugin->getLibraries($editor), $libraries, function($a, $b) {
......
...@@ -74,8 +74,8 @@ function testEnabledPlugins() { ...@@ -74,8 +74,8 @@ function testEnabledPlugins() {
'drupalimage' => 'core/modules/ckeditor/js/plugins/drupalimage/plugin.js', 'drupalimage' => 'core/modules/ckeditor/js/plugins/drupalimage/plugin.js',
'drupallink' => 'core/modules/ckeditor/js/plugins/drupallink/plugin.js', 'drupallink' => 'core/modules/ckeditor/js/plugins/drupallink/plugin.js',
); );
$this->assertIdentical($enabled_plugins, $this->manager->getEnabledPlugins($editor), 'Only built-in plugins are enabled.'); $this->assertIdentical($enabled_plugins, $this->manager->getEnabledPluginFiles($editor), 'Only built-in plugins are enabled.');
$this->assertIdentical(array('internal' => NULL) + $enabled_plugins, $this->manager->getEnabledPlugins($editor, TRUE), 'Only the "internal" plugin is enabled.'); $this->assertIdentical(array('internal' => NULL) + $enabled_plugins, $this->manager->getEnabledPluginFiles($editor, TRUE), 'Only the "internal" plugin is enabled.');
// Enable the CKEditor Test module, which has the Llama plugin (plus three // Enable the CKEditor Test module, which has the Llama plugin (plus three
// variations of it, to cover all possible ways a plugin can be enabled) and // variations of it, to cover all possible ways a plugin can be enabled) and
...@@ -87,8 +87,8 @@ function testEnabledPlugins() { ...@@ -87,8 +87,8 @@ function testEnabledPlugins() {
$plugin_ids = array_keys($this->manager->getDefinitions()); $plugin_ids = array_keys($this->manager->getDefinitions());
sort($plugin_ids); sort($plugin_ids);
$this->assertIdentical(array('drupalimage', 'drupallink', 'internal', 'llama', 'llama_button', 'llama_contextual', 'llama_contextual_and_button', 'stylescombo'), $plugin_ids, 'Additional CKEditor plugins found.'); $this->assertIdentical(array('drupalimage', 'drupallink', 'internal', 'llama', 'llama_button', 'llama_contextual', 'llama_contextual_and_button', 'stylescombo'), $plugin_ids, 'Additional CKEditor plugins found.');
$this->assertIdentical($enabled_plugins, $this->manager->getEnabledPlugins($editor), 'Only the internal plugins are enabled.'); $this->assertIdentical($enabled_plugins, $this->manager->getEnabledPluginFiles($editor), 'Only the internal plugins are enabled.');
$this->assertIdentical(array('internal' => NULL) + $enabled_plugins, $this->manager->getEnabledPlugins($editor, TRUE), 'Only the "internal" plugin is enabled.'); $this->assertIdentical(array('internal' => NULL) + $enabled_plugins, $this->manager->getEnabledPluginFiles($editor, TRUE), 'Only the "internal" plugin is enabled.');
// Case 3: enable each of the newly available plugins, if possible: // Case 3: enable each of the newly available plugins, if possible:
// a. Llama: cannot be enabled, since it does not implement // a. Llama: cannot be enabled, since it does not implement
...@@ -111,19 +111,19 @@ function testEnabledPlugins() { ...@@ -111,19 +111,19 @@ function testEnabledPlugins() {
$file['c'] = 'core/modules/ckeditor/tests/modules/js/llama_contextual.js'; $file['c'] = 'core/modules/ckeditor/tests/modules/js/llama_contextual.js';
$file['cb'] = 'core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js'; $file['cb'] = 'core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js';
$expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual_and_button' => $file['cb']); $expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual_and_button' => $file['cb']);
$this->assertIdentical($expected, $this->manager->getEnabledPlugins($editor), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.'); $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.');
$this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPlugins($editor, TRUE), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.'); $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.');
$editor->settings['toolbar']['buttons'][0] = $original_toolbar; $editor->settings['toolbar']['buttons'][0] = $original_toolbar;
$editor->settings['toolbar']['buttons'][0][] = 'Strike'; $editor->settings['toolbar']['buttons'][0][] = 'Strike';
$editor->save(); $editor->save();
$expected = $enabled_plugins + array('llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']); $expected = $enabled_plugins + array('llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']);
$this->assertIdentical($expected, $this->manager->getEnabledPlugins($editor), 'The LLamaContextual and LlamaContextualAndButton plugins are enabled.'); $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LLamaContextual and LlamaContextualAndButton plugins are enabled.');
$this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPlugins($editor, TRUE), 'The LlamaContextual and LlamaContextualAndButton plugins are enabled.'); $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LlamaContextual and LlamaContextualAndButton plugins are enabled.');
$editor->settings['toolbar']['buttons'][0][] = 'Llama'; $editor->settings['toolbar']['buttons'][0][] = 'Llama';
$editor->save(); $editor->save();
$expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']); $expected = $enabled_plugins + array('llama_button' => $file['b'], 'llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']);
$this->assertIdentical($expected, $this->manager->getEnabledPlugins($editor), 'The LlamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.'); $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.');
$this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPlugins($editor, TRUE), 'The LLamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.'); $this->assertIdentical(array('internal' => NULL) + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LLamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.');
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment