core/modules/editor/editor.api.php | 23 +++---
core/modules/editor/editor.js | 80 --------------------
core/modules/editor/editor.module | 58 +++++++++++---
.../Drupal/editor/Plugin/Core/Entity/Editor.php | 5 +-
.../lib/Drupal/editor/Plugin/EditorInterface.php | 37 +++++++--
.../lib/Drupal/editor/Plugin/EditorManager.php | 16 ++--
6 files changed, 100 insertions(+), 119 deletions(-)
diff --git a/core/modules/editor/editor.api.php b/core/modules/editor/editor.api.php
index cbca0c7..c4ca2cb 100644
--- a/core/modules/editor/editor.api.php
+++ b/core/modules/editor/editor.api.php
@@ -13,21 +13,19 @@
/**
* Perform alterations on text editor definitions.
*
- * @param $editors
- * Array of information on editors exposed by hook_editor_info()
- * implementations.
+ * @param array $editors
+ * An array of information on text editors, as collected by the annotation
+ * discovery mechanism.
+ *
+ * @see \Drupal\editor\Plugin\EditorBase
*/
-function hook_editor_info_alter(&$editors) {
+function hook_editor_info_alter(array &$editors) {
$editors['some_other_editor']['title'] = t('A different name');
$editors['some_other_editor']['library']['module'] = 'myeditoroverride';
}
/**
- * Perform alterations on the JavaScript settings that are added for text formats.
- *
- * Note that changing settings here only affects the client side behavior of the
- * filter. To affect the filter globally both on the client side and server
- * side, use hook_filter_info_alter().
+ * Modifies JavaScript settings that are added for text editors.
*
* @param array $settings
* All the settings that will be added to the page via drupal_add_js() for
@@ -35,11 +33,10 @@ function hook_editor_info_alter(&$editors) {
* @param array $formats
* The list of format objects for which settings are being added.
*/
-function hook_editor_js_settings_alter(&$settings, $formats) {
+function hook_editor_js_settings_alter(array &$settings, array $formats) {
if (isset($formats['filtered_html'])) {
- $settings['filtered_html']['allowedTags'][] = 'strong';
- $settings['filtered_html']['allowedTags'][] = 'em';
- $settings['filtered_html']['allowedTags'][] = 'img';
+ $settings['filtered_html']['editor'][] = 'MyDifferentEditor';
+ $settings['filtered_html']['editorSettings']['buttons'] = array('strong', 'italic', 'underline');
}
}
diff --git a/core/modules/editor/editor.js b/core/modules/editor/editor.js
deleted file mode 100644
index 3759284..0000000
--- a/core/modules/editor/editor.js
+++ /dev/null
@@ -1,80 +0,0 @@
-/**
- * @file
- * Attaches behavior for the Editor module.
- */
-
-(function ($, Drupal, drupalSettings) {
-
-"use strict";
-
-/**
- * Initialize an empty object where editors where place their attachment code.
- */
-Drupal.editors = {};
-
-/**
- * Enables editors on text_format elements.
- */
-Drupal.behaviors.editors = {
- attach: function (context, settings) {
- // If there are no editor settings, there are no editors to enable.
- if (!settings.editor) {
- return;
- }
-
- var $context = $(context);
- $context.find('.filter-list:input').once('editors', function () {
- var $this = $(this);
- var activeEditor = $this.val();
- var field = $this.closest('.text-format-wrapper').find('textarea').get(-1);
-
- // Directly attach this editor, if the text format is enabled or there is
- // only one text format at all.
- if ($this.is(':input')) {
- if (drupalSettings.editor.formats[activeEditor]) {
- Drupal.editorAttach(field, drupalSettings.editor.formats[activeEditor]);
- }
- }
- // Attach onChange handlers to text format selector elements.
- if ($this.is('select')) {
- $this.on('change.editorAttach', function () {
- // Prevent double-binding if the change event is triggered manually.
- if ($this.val() === activeEditor) {
- return;
- }
-
- // Detach the current editor (if any) and attach a new editor.
- if (drupalSettings.editor.formats[activeEditor]) {
- Drupal.editorDetach(field, drupalSettings.editor.formats[activeEditor]);
- }
- activeEditor = $this.val();
- if (drupalSettings.editor.formats[activeEditor]) {
- Drupal.editorAttach(field, drupalSettings.editor.formats[activeEditor]);
- }
- });
- }
- // Detach any editor when the containing form is submitted.
- $this.parents('form').submit(function (event) {
- // Do not detach if the event was canceled.
- if (event.isDefaultPrevented()) {
- return;
- }
- Drupal.editorDetach(field, drupalSettings.editor.formats[activeEditor]);
- });
- });
- }
-};
-
-Drupal.editorAttach = function (field, format) {
- if (format.editor) {
- Drupal.editors[format.editor].attach(field, format);
- }
-};
-
-Drupal.editorDetach = function (field, format) {
- if (format.editor) {
- Drupal.editors[format.editor].detach(field, format);
- }
-};
-
-})(jQuery, Drupal, drupalSettings);
diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module
index 4d817b3..b115693 100644
--- a/core/modules/editor/editor.module
+++ b/core/modules/editor/editor.module
@@ -2,11 +2,36 @@
/**
* @file
- * Provides bindings between Filter module text formats and client-side editors.
+ * Adds bindings for client-side "text editors" to text formats.
*/
use Drupal\file\Plugin\Core\Entity\File;
use Drupal\editor\Plugin\Core\Entity\Editor;
+use Drupal\Component\Utility\NestedArray;
+
+/**
+ * Implements hook_help().
+ */
+function editor_help($path, $arg) {
+ switch ($path) {
+ case 'admin/help#editor':
+ $output = '';
+ $output .= '
' . t('About') . '
';
+ $output .= '' . t('The Text Editor module provides a framework to extend the user interface on text fields that allow HTML input. Without Text Editor module, fields accept only text where formatting must be typed manually, such as entering a <strong> tag to make text bold or an <em> tag to italicize text. The Text Editor module allows these fields to be enhanced with rich text editors (WYSIWYGs) or toolbars, which make entering and formatting content easier. For more information, see the online handbook entry for Editor module.', array('@editor' => 'http://drupal.org/documentation/modules/editor/')) . '
';
+ $output .= '' . t('Uses') . '
';
+ $output .= '';
+ $output .= '- ' . t('Enabling or configuring a text editor') . '
';
+ $output .= '- ' . t('The Text Editor module does not have its own configuration page. Instead it enhances existing configuration pages with additional options. Text editors are attached to individual text formats, which can be configured on the Text formats page. Each text format may be associated with a single text editor. When entering content with that text format, the associated text editor will automatically be enabled.', array('@formats' => url('admin/config/content/formats'))) . '
';
+ $output .= '- ' . t('Allowing a user to choose a text editor') . '
';
+ $output .= '- ' . t('Because text editor configurations are bound to a text format, users with access to more than one text format may switch between available text editors by changing the text format for a field. For more information about text formats, see the Filter module help page, which describes text formats in more detail.', array('@filter' => url('admin/help/filter'))) . '
';
+ // @todo: Mention the availability of the built-in core WYSIWYG (CKEditor)
+ // when it becomes available. See http://drupal.org/node/1878344.
+ $output .= '- ' . t('Installing additional text editor libraries') . '
';
+ $output .= '- ' . t('The Text Editor module does not provide any text editor libraries itself. Most installations of Drupal include a module that provides a text editor library which may be enabled on the Modules page. Additional modules that provide text editor libraries may be downloaded from Drupal.org.', array('@modules' => url('admin/modules'), '@download' => 'http://drupal.org/search/site/wysiwyg%20module')) . '
';
+ $output .= '
';
+ return $output;
+ }
+}
/**
* Implements hook_menu_alter().
@@ -39,11 +64,12 @@ function editor_element_info() {
* Implements hook_library_info().
*/
function editor_library_info() {
+ $path = drupal_get_path('module', 'editor');
$libraries['drupal.editor'] = array(
'title' => 'Editor',
'version' => VERSION,
'js' => array(
- drupal_get_path('module', 'editor') . '/editor.js' => array('group' => JS_LIBRARY),
+ $path . '/js/editor.js' => array('group' => JS_LIBRARY),
),
'dependencies' => array(
array('system', 'jquery'),
@@ -60,16 +86,17 @@ function editor_library_info() {
* Implements hook_form_FORM_ID_alter().
*/
function editor_form_filter_admin_overview_alter(&$form, $form_state) {
+ // @todo Cleanup column injection: http://drupal.org/node/1876718
// Splice in the column for "Associated editor" into the header.
$position = array_search('name', $form['formats']['#header']) + 1;
$start = array_splice($form['formats']['#header'], 0, $position, array('editor' => t('Associated editor')));
$form['formats']['#header'] = array_merge($start, $form['formats']['#header']);
// Then splice in the name of each text editor for each text format.
- $editors = drupal_container()->get('plugin.manager.editor')->listAvailableDefinitions();
+ $editors = drupal_container()->get('plugin.manager.editor')->getDefinitions();
foreach (element_children($form['formats']) as $format_id) {
$editor = editor_load($format_id);
- $editor_name = $editor && isset($editors[$editor->editor]) ? $editors[$editor->editor] : drupal_placeholder(t('None'));
+ $editor_name = ($editor && isset($editors[$editor->editor])) ? $editors[$editor->editor]['title'] : drupal_placeholder(t('None'));
$editor_column['editor'] = array(
'#type' => 'markup',
'#markup' => $editor_name,
@@ -103,12 +130,15 @@ function editor_form_filter_admin_format_form_alter(&$form, &$form_state) {
}
// Associate a text editor with this text format.
- $editor_options = array('' => t('None')) + $manager->listAvailableDefinitions();
+ $editor_options = $manager->listOptions();
$form['editor'] = array(
'#type' => 'select',
'#title' => t('Editor'),
'#options' => $editor_options,
+ '#empty_option' => t('None'),
'#default_value' => $editor ? $editor->editor : '',
+ // Position the editor selection before the filter settings (weight of 0),
+ // but after the filter label and name (weight of -20).
'#weight' => -9,
'#ajax' => array(
'callback' => 'editor_form_filter_admin_form_ajax',
@@ -117,7 +147,7 @@ function editor_form_filter_admin_format_form_alter(&$form, &$form_state) {
);
// If there aren't any options (other than "None") disable the select list.
- if (count($editor_options) === 1) {
+ if (empty($editor_options)) {
$form['editor']['#disabled'] = TRUE;
$form['editor']['#description'] = t('This option is disabled because no modules that provide an editor are currently enabled.');
}
@@ -129,10 +159,15 @@ function editor_form_filter_admin_format_form_alter(&$form, &$form_state) {
'#prefix' => '',
'#suffix' => '
',
);
+
+ // Add validation per-editor validation and submit handlers.
if ($editor) {
$plugin = $manager->createInstance($editor->editor);
$form['editor_settings'] = array_merge($form['editor_settings'], $plugin->settingsForm($form, $form_state, $editor));
+ // Place validation first since editor validation errors should appear above
+ // individual filter validation errors.
array_unshift($form['#validate'], array($plugin, 'settingsFormValidate'));
+ $form['#submit'][] = array($plugin, 'settingsFormSubmit');
}
$form['#submit'][] = 'editor_form_filter_admin_format_submit';
@@ -181,12 +216,16 @@ function editor_form_filter_admin_format_submit($form, &$form_state) {
* A text editor object, or FALSE.
*/
function editor_load($format_id) {
+ // Load all the editors at once here, assuming that either no editors or more
+ // than one editor will be needed on a page (such as having multiple text
+ // formats for administrators). Loading a small number of editors all at once
+ // is more efficient than loading multiple editors individually.
$editors = entity_load_multiple('editor');
return isset($editors[$format_id]) ? $editors[$format_id] : FALSE;
}
/**
- * Additional #process callback on 'text_format' elements.
+ * Additional #process callback for 'text_format' elements.
*/
function editor_process_format($element) {
global $user;
@@ -196,7 +235,7 @@ function editor_process_format($element) {
if (!isset($element['#attached'])) {
$element['#attached'] = array();
}
- $element['#attached'] = array_merge_recursive($element['#attached'], editor_get_attachments($formats));
+ $element['#attached'] = NestedArray::mergeDeep($element['#attached'], editor_get_attachments($formats));
return $element;
}
@@ -217,6 +256,7 @@ function editor_get_attachments($formats) {
$manager = drupal_container()->get('plugin.manager.editor');
$definitions = $manager->getDefinitions();
+ $settings = array();
foreach ($formats as $format_id => $format_info) {
$editor = editor_load($format_id);
if (!$editor) {
@@ -231,7 +271,7 @@ function editor_get_attachments($formats) {
$plugin = $manager->createInstance($editor->editor);
$settings[$format_id] = array(
'editor' => $editor->editor,
- 'editorSettings' => $plugin->generateJsSettings($editor),
+ 'editorSettings' => $plugin->generateJSSettings($editor),
);
}
diff --git a/core/modules/editor/lib/Drupal/editor/Plugin/Core/Entity/Editor.php b/core/modules/editor/lib/Drupal/editor/Plugin/Core/Entity/Editor.php
index 9a32b80..0ed6c3a 100644
--- a/core/modules/editor/lib/Drupal/editor/Plugin/Core/Entity/Editor.php
+++ b/core/modules/editor/lib/Drupal/editor/Plugin/Core/Entity/Editor.php
@@ -2,7 +2,7 @@
/**
* @file
- * Definition of \Drupal\editor\Plugin\Core\Entity\Editor.
+ * Contains \Drupal\editor\Plugin\Core\Entity\Editor.
*/
namespace Drupal\editor\Plugin\Core\Entity;
@@ -23,7 +23,6 @@
* entity_keys = {
* "id" = "format",
* "label" = "name",
- * "editor" = "editor",
* "uuid" = "uuid"
* }
* )
@@ -31,7 +30,7 @@
class Editor extends ConfigEntityBase {
/**
- * The text format to which this text editor is bound.
+ * The machine name of the text format to which this text editor is bound.
*
* @var string
*/
diff --git a/core/modules/editor/lib/Drupal/editor/Plugin/EditorInterface.php b/core/modules/editor/lib/Drupal/editor/Plugin/EditorInterface.php
index 7b0a537..b1df816 100644
--- a/core/modules/editor/lib/Drupal/editor/Plugin/EditorInterface.php
+++ b/core/modules/editor/lib/Drupal/editor/Plugin/EditorInterface.php
@@ -2,7 +2,7 @@
/**
* @file
- * Definition of \Drupal\editor\Plugin\EditorInterface.
+ * Contains \Drupal\editor\Plugin\EditorInterface.
*/
namespace Drupal\editor\Plugin;
@@ -12,6 +12,10 @@
/**
* Defines an interface for configurable text editors.
+ *
+ * Modules implementing this interface should instead extend the EditorBase
+ * class, which provides default implementations of each method where
+ * appropriate.
*/
interface EditorInterface extends PluginInspectionInterface {
@@ -27,10 +31,12 @@ function defaultSettings();
/**
* Returns a settings form to configure this text editor.
*
- * Note that this form will be a subform of filter_admin_format_form(). Hence
- * the Filter module will take care of saving the settings.
+ * Note that this form will be a subform of filter_admin_format_form().
+ * Settings within this subform are validated by settingsFormValidate() and
+ * then saved exactly as specified in $form_state['values'] by
+ * editor_form_filter_admin_format_submit().
*
- * If the editor's behavior depends on an extensive list and/or external data,
+ * If the editor's behavior depends on extensive options and/or external data,
* then the implementing module can choose to provide a separate, global
* configuration page rather than per-text-format settings. In that case, this
* form should provide a link to the separate settings page.
@@ -38,7 +44,7 @@ function defaultSettings();
* @param array $form
* The prepopulated form array of the filter administration form.
* @param array $form_state
- * The state of the (entire) configuration form.
+ * The state of the entire configuration form.
* @param \Drupal\editor\Plugin\Core\Entity\Editor $editor
* A configured text editor object.
* @return array
@@ -47,7 +53,11 @@ function defaultSettings();
function settingsForm(array &$form, array &$form_state, Editor $editor);
/**
- * Validates the settings form.
+ * Validates the settings form for an editor.
+ *
+ * The contents of the editor settings are located in
+ * $form_state['values']['editor_settings']. Calls to form_set_error() should
+ * reflect this location in the settings form.
*
* @param array $form
* An associative array containing the structure of the form.
@@ -57,6 +67,19 @@ function settingsForm(array &$form, array &$form_state, Editor $editor);
function settingsFormValidate(array $form, array &$form_state);
/**
+ * Modifies any values in the form state to prepare them for saving.
+ *
+ * Values in $form_state['values']['editor_settings'] are saved by Editor
+ * module in editor_form_filter_admin_format_submit().
+ *
+ * @param array $form
+ * An associative array containing the structure of the form.
+ * @param array $form_state
+ * A reference to a keyed array containing the current state of the form.
+ */
+ function settingsFormSubmit(array $form, array &$form_state);
+
+ /**
* Generates JavaScript settings for a configured text editor object.
*
* Most text editors use JavaScript to provide a WYSIWYG or toolbar on the
@@ -70,6 +93,6 @@ function settingsFormValidate(array $form, array &$form_state);
* An array of settings that will be added to the page for use by this text
* editor's JavaScript integration.
*/
- function generateJsSettings(Editor $editor);
+ function generateJSSettings(Editor $editor);
}
diff --git a/core/modules/editor/lib/Drupal/editor/Plugin/EditorManager.php b/core/modules/editor/lib/Drupal/editor/Plugin/EditorManager.php
index 2dab4a7..c391540 100644
--- a/core/modules/editor/lib/Drupal/editor/Plugin/EditorManager.php
+++ b/core/modules/editor/lib/Drupal/editor/Plugin/EditorManager.php
@@ -2,7 +2,7 @@
/**
* @file
- * Definition of \Drupal\editor\Plugin\EditorManager.
+ * Contains \Drupal\editor\Plugin\EditorManager.
*/
namespace Drupal\editor\Plugin;
@@ -29,14 +29,16 @@ public function __construct() {
}
/**
- * List all available plugin definitions.
+ * Populate a key-value pair of available text editors.
*
* @return array
- * An array of available definition titles, keyed by id.
+ * An array of translated text editor labels, keyed by ID.
*/
- public function listAvailableDefinitions() {
- return array_reduce($this->getDefinitions(), function($result, $definition) {
- return $result + array($definition['id'] => $definition['title']);
- }, array());
+ public function listOptions() {
+ $options = array();
+ foreach ($this->getDefinitions() as $key => $definition) {
+ $options[$key] = $definition['title'];
+ }
+ return $options;
}
}