CKEditorPluginManager.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace Drupal\ckeditor;
  3. use Drupal\Component\Utility\NestedArray;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Plugin\DefaultPluginManager;
  6. use Drupal\Core\Cache\CacheBackendInterface;
  7. use Drupal\Core\Extension\ModuleHandlerInterface;
  8. use Drupal\editor\Entity\Editor;
  9. /**
  10. * Provides a CKEditor Plugin plugin manager.
  11. *
  12. * @see \Drupal\ckeditor\CKEditorPluginInterface
  13. * @see \Drupal\ckeditor\CKEditorPluginButtonsInterface
  14. * @see \Drupal\ckeditor\CKEditorPluginContextualInterface
  15. * @see \Drupal\ckeditor\CKEditorPluginConfigurableInterface
  16. * @see \Drupal\ckeditor\CKEditorPluginCssInterface
  17. * @see \Drupal\ckeditor\CKEditorPluginBase
  18. * @see \Drupal\ckeditor\Annotation\CKEditorPlugin
  19. * @see plugin_api
  20. */
  21. class CKEditorPluginManager extends DefaultPluginManager {
  22. /**
  23. * Constructs a CKEditorPluginManager object.
  24. *
  25. * @param \Traversable $namespaces
  26. * An object that implements \Traversable which contains the root paths
  27. * keyed by the corresponding namespace to look for plugin implementations.
  28. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  29. * Cache backend instance to use.
  30. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  31. * The module handler to invoke the alter hook with.
  32. */
  33. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
  34. parent::__construct('Plugin/CKEditorPlugin', $namespaces, $module_handler, 'Drupal\ckeditor\CKEditorPluginInterface', 'Drupal\ckeditor\Annotation\CKEditorPlugin');
  35. $this->alterInfo('ckeditor_plugin_info');
  36. $this->setCacheBackend($cache_backend, 'ckeditor_plugins');
  37. }
  38. /**
  39. * Retrieves enabled plugins' files, keyed by plugin ID.
  40. *
  41. * For CKEditor plugins that implement:
  42. * - CKEditorPluginButtonsInterface, not CKEditorPluginContextualInterface,
  43. * a plugin is enabled if at least one of its buttons is in the toolbar;
  44. * - CKEditorPluginContextualInterface, not CKEditorPluginButtonsInterface,
  45. * a plugin is enabled if its isEnabled() method returns TRUE
  46. * - both of these interfaces, a plugin is enabled if either is the case.
  47. *
  48. * Internal plugins (those that are part of the bundled build of CKEditor) are
  49. * excluded by default, since they are loaded implicitly. If you need to know
  50. * even implicitly loaded (i.e. internal) plugins, then set the optional
  51. * second parameter.
  52. *
  53. * @param \Drupal\editor\Entity\Editor $editor
  54. * A configured text editor object.
  55. * @param bool $include_internal_plugins
  56. * Defaults to FALSE. When set to TRUE, plugins whose isInternal() method
  57. * returns TRUE will also be included.
  58. *
  59. * @return array
  60. * A list of the enabled CKEditor plugins, with the plugin IDs as keys and
  61. * the Drupal root-relative plugin files as values.
  62. * For internal plugins, the value is NULL.
  63. */
  64. public function getEnabledPluginFiles(Editor $editor, $include_internal_plugins = FALSE) {
  65. $plugins = array_keys($this->getDefinitions());
  66. $toolbar_buttons = $this->getEnabledButtons($editor);
  67. $enabled_plugins = [];
  68. $additional_plugins = [];
  69. foreach ($plugins as $plugin_id) {
  70. $plugin = $this->createInstance($plugin_id);
  71. if (!$include_internal_plugins && $plugin->isInternal()) {
  72. continue;
  73. }
  74. $enabled = FALSE;
  75. // Enable this plugin if it provides a button that has been enabled.
  76. if ($plugin instanceof CKEditorPluginButtonsInterface) {
  77. $plugin_buttons = array_keys($plugin->getButtons());
  78. $enabled = (count(array_intersect($toolbar_buttons, $plugin_buttons)) > 0);
  79. }
  80. // Otherwise enable this plugin if it declares itself as enabled.
  81. if (!$enabled && $plugin instanceof CKEditorPluginContextualInterface) {
  82. $enabled = $plugin->isEnabled($editor);
  83. }
  84. if ($enabled) {
  85. $enabled_plugins[$plugin_id] = ($plugin->isInternal()) ? NULL : $plugin->getFile();
  86. // Check if this plugin has dependencies that also need to be enabled.
  87. $additional_plugins = array_merge($additional_plugins, array_diff($plugin->getDependencies($editor), $additional_plugins));
  88. }
  89. }
  90. // Add the list of dependent plugins.
  91. foreach ($additional_plugins as $plugin_id) {
  92. $plugin = $this->createInstance($plugin_id);
  93. $enabled_plugins[$plugin_id] = ($plugin->isInternal()) ? NULL : $plugin->getFile();
  94. }
  95. // Always return plugins in the same order.
  96. asort($enabled_plugins);
  97. return $enabled_plugins;
  98. }
  99. /**
  100. * Gets the enabled toolbar buttons in the given text editor instance.
  101. *
  102. * @param \Drupal\editor\Entity\Editor $editor
  103. * A configured text editor object.
  104. *
  105. * @return string[]
  106. * A list of the toolbar buttons enabled in the given text editor instance.
  107. */
  108. public static function getEnabledButtons(Editor $editor) {
  109. $toolbar_rows = [];
  110. $settings = $editor->getSettings();
  111. foreach ($settings['toolbar']['rows'] as $row_number => $row) {
  112. $toolbar_rows[] = array_reduce($settings['toolbar']['rows'][$row_number], function ($result, $button_group) {
  113. return array_merge($result, $button_group['items']);
  114. }, []);
  115. }
  116. return array_unique(NestedArray::mergeDeepArray($toolbar_rows));
  117. }
  118. /**
  119. * Retrieves all available CKEditor buttons, keyed by plugin ID.
  120. *
  121. * @return array
  122. * All available CKEditor buttons, with plugin IDs as keys and button
  123. * metadata (as implemented by getButtons()) as values.
  124. *
  125. * @see \Drupal\ckeditor\CKEditorPluginButtonsInterface::getButtons()
  126. */
  127. public function getButtons() {
  128. $plugins = array_keys($this->getDefinitions());
  129. $buttons_plugins = [];
  130. foreach ($plugins as $plugin_id) {
  131. $plugin = $this->createInstance($plugin_id);
  132. if ($plugin instanceof CKEditorPluginButtonsInterface) {
  133. $buttons_plugins[$plugin_id] = $plugin->getButtons();
  134. }
  135. }
  136. return $buttons_plugins;
  137. }
  138. /**
  139. * Injects the CKEditor plugins settings forms as a vertical tabs subform.
  140. *
  141. * @param array &$form
  142. * A reference to an associative array containing the structure of the form.
  143. * @param \Drupal\Core\Form\FormStateInterface $form_state
  144. * The current state of the form.
  145. * @param \Drupal\editor\Entity\Editor $editor
  146. * A configured text editor object.
  147. */
  148. public function injectPluginSettingsForm(array &$form, FormStateInterface $form_state, Editor $editor) {
  149. $definitions = $this->getDefinitions();
  150. foreach (array_keys($definitions) as $plugin_id) {
  151. $plugin = $this->createInstance($plugin_id);
  152. if ($plugin instanceof CKEditorPluginConfigurableInterface) {
  153. $plugin_settings_form = [];
  154. $form['plugins'][$plugin_id] = [
  155. '#type' => 'details',
  156. '#title' => $definitions[$plugin_id]['label'],
  157. '#open' => TRUE,
  158. '#group' => 'editor][settings][plugin_settings',
  159. '#attributes' => [
  160. 'data-ckeditor-plugin-id' => $plugin_id,
  161. ],
  162. ];
  163. // Provide enough metadata for the drupal.ckeditor.admin library to
  164. // allow it to automatically show/hide the vertical tab containing the
  165. // settings for this plugin. Only do this if it's a CKEditor plugin that
  166. // just provides buttons, don't do this if it's a contextually enabled
  167. // CKEditor plugin. After all, in the latter case, we can't know when
  168. // its settings should be shown!
  169. if ($plugin instanceof CKEditorPluginButtonsInterface && !$plugin instanceof CKEditorPluginContextualInterface) {
  170. $form['plugins'][$plugin_id]['#attributes']['data-ckeditor-buttons'] = implode(' ', array_keys($plugin->getButtons()));
  171. }
  172. $form['plugins'][$plugin_id] += $plugin->settingsForm($plugin_settings_form, $form_state, $editor);
  173. }
  174. }
  175. }
  176. /**
  177. * Retrieves enabled plugins' iframe instance CSS files, keyed by plugin ID.
  178. *
  179. * @param \Drupal\editor\Entity\Editor $editor
  180. * A configured text editor object.
  181. *
  182. * @return string[]
  183. * Enabled plugins CKEditor CSS files, with plugin IDs as keys and CSS file
  184. * paths relative to the Drupal root (as implemented by getCssFiles()) as
  185. * values.
  186. *
  187. * @see \Drupal\ckeditor\CKEditorPluginCssInterface::getCssFiles()
  188. */
  189. public function getCssFiles(Editor $editor) {
  190. $enabled_plugins = array_keys($this->getEnabledPluginFiles($editor, TRUE));
  191. $css_files = [];
  192. foreach ($enabled_plugins as $plugin_id) {
  193. $plugin = $this->createInstance($plugin_id);
  194. if ($plugin instanceof CKEditorPluginCssInterface) {
  195. $css_files[$plugin_id] = $plugin->getCssFiles($editor);
  196. }
  197. }
  198. return $css_files;
  199. }
  200. }