editor.module

Adds bindings for client-side "text editors" to text formats.

File

drupal/core/modules/editor/editor.module
View source
<?php

/**
 * @file
 * 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 .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . 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 <code>&lt;strong&gt;</code> tag to make text bold or an <code>&lt;em&gt;</code> 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 <a href="@editor">Editor module</a>.', array(
        '@editor' => 'http://drupal.org/documentation/modules/editor/',
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Enabling or configuring a text editor') . '</dt>';
      $output .= '<dd>' . 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 <a href="@formats">Text formats page</a>. 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'),
      )) . '</dd>';
      $output .= '<dt>' . t('Allowing a user to choose a text editor') . '</dt>';
      $output .= '<dd>' . 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 <a href="@filter">Filter module help page</a>, which describes text formats in more detail.', array(
        '@filter' => url('admin/help/filter'),
      )) . '</dd>';

      // @todo: Mention the availability of the built-in core WYSIWYG (CKEditor)
      // when it becomes available. See http://drupal.org/node/1878344.
      $output .= '<dt>' . t('Installing additional text editor libraries') . '</dt>';
      $output .= '<dd>' . 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 <a href="@modules">Modules page</a>. Additional modules that provide text editor libraries may be <a href="@download">downloaded from Drupal.org</a>.', array(
        '@modules' => url('admin/modules'),
        '@download' => 'http://drupal.org/search/site/wysiwyg%20module',
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_menu_alter().
 *
 * Rewrites the menu entries for filter module that relate to the configuration
 * of text editors.
 */
function editor_menu_alter(&$items) {
  $items['admin/config/content/formats']['title'] = 'Text formats and editors';
  $items['admin/config/content/formats']['description'] = 'Configure how user-contributed content is filtered and formatted, as well as the text editor user interface (WYSIWYGs or toolbars).';
}

/**
 * Implements hook_element_info().
 *
 * Extends the functionality of text_format elements (provided by Filter
 * module), so that selecting a text format notifies a client-side text editor
 * when it should be enabled or disabled.
 *
 * @see filter_element_info()
 */
function editor_element_info() {
  $type['text_format'] = array(
    '#pre_render' => array(
      'editor_pre_render_format',
    ),
  );
  return $type;
}

/**
 * Implements hook_library_info().
 */
function editor_library_info() {
  $path = drupal_get_path('module', 'editor');
  $libraries['drupal.editor'] = array(
    'title' => 'Text Editor',
    'version' => VERSION,
    'js' => array(
      $path . '/js/editor.js' => array(),
    ),
    'dependencies' => array(
      array(
        'system',
        'jquery',
      ),
      array(
        'system',
        'drupal',
      ),
      array(
        'system',
        'drupalSettings',
      ),
      array(
        'system',
        'jquery.once',
      ),
    ),
  );
  $libraries['edit.formattedTextEditor.editor'] = array(
    'title' => 'Formatted text editor',
    'version' => VERSION,
    'js' => array(
      $path . '/js/editor.formattedTextEditor.js' => array(
        'scope' => 'footer',
        'attributes' => array(
          'defer' => TRUE,
        ),
      ),
      array(
        'type' => 'setting',
        'data' => array(
          'editor' => array(
            'getUntransformedTextURL' => url('editor/!entity_type/!id/!field_name/!langcode/!view_mode'),
          ),
        ),
      ),
    ),
    'dependencies' => array(
      array(
        'edit',
        'edit',
      ),
      array(
        'editor',
        'drupal.editor',
      ),
      array(
        'system',
        'drupal.ajax',
      ),
      array(
        'system',
        'drupalSettings',
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_menu().
 */
function editor_menu() {

  // @todo Remove this menu item in http://drupal.org/node/1954892 when theme
  //   callbacks are replaced with something else.
  $items['editor/%/%/%/%/%'] = array(
    'route_name' => 'editor_field_untransformed_text',
    'theme callback' => 'ajax_base_page_theme',
  );
  return $items;
}

/**
 * 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 "Text editor" into the header.
  $position = array_search('name', $form['formats']['#header']) + 1;
  $start = array_splice($form['formats']['#header'], 0, $position, array(
    'editor' => t('Text 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')
    ->getDefinitions();
  foreach (element_children($form['formats']) as $format_id) {
    $editor = editor_load($format_id);
    $editor_name = $editor && isset($editors[$editor->editor]) ? $editors[$editor->editor]['label'] : drupal_placeholder('—');
    $editor_column['editor'] = array(
      '#markup' => $editor_name,
    );
    $position = array_search('name', array_keys($form['formats'][$format_id])) + 1;
    $start = array_splice($form['formats'][$format_id], 0, $position, $editor_column);
    $form['formats'][$format_id] = array_merge($start, $form['formats'][$format_id]);
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function editor_form_filter_admin_format_form_alter(&$form, &$form_state) {
  if (!isset($form_state['editor'])) {
    $format_id = $form['#format']->format;
    $form_state['editor'] = editor_load($format_id);
    $form_state['editor_manager'] = drupal_container()
      ->get('plugin.manager.editor');
  }
  $editor = $form_state['editor'];
  $manager = $form_state['editor_manager'];

  // Associate a text editor with this text format.
  $editor_options = $manager
    ->listOptions();
  $form['editor'] = array(
    // Position the editor selection before the filter settings (weight of 0),
    // but after the filter label and name (weight of -20).
    '#weight' => -9,
  );
  $form['editor']['editor'] = array(
    '#type' => 'select',
    '#title' => t('Text editor'),
    '#options' => $editor_options,
    '#empty_option' => t('None'),
    '#default_value' => $editor ? $editor->editor : '',
    '#ajax' => array(
      'trigger_as' => array(
        'name' => 'editor_configure',
      ),
      'callback' => 'editor_form_filter_admin_form_ajax',
      'wrapper' => 'editor-settings-wrapper',
    ),
    '#weight' => -10,
  );
  $form['editor']['configure'] = array(
    '#type' => 'submit',
    '#name' => 'editor_configure',
    '#value' => t('Configure'),
    '#limit_validation_errors' => array(
      array(
        'editor',
      ),
    ),
    '#submit' => array(
      'editor_form_filter_admin_format_editor_configure',
    ),
    '#ajax' => array(
      'callback' => 'editor_form_filter_admin_form_ajax',
      'wrapper' => 'editor-settings-wrapper',
    ),
    '#weight' => -10,
    '#attributes' => array(
      'class' => array(
        'js-hide',
      ),
    ),
  );

  // If there aren't any options (other than "None"), disable the select list.
  if (empty($editor_options)) {
    $form['editor']['editor']['#disabled'] = TRUE;
    $form['editor']['editor']['#description'] = t('This option is disabled because no modules that provide a text editor are currently enabled.');
  }
  $form['editor']['settings'] = array(
    '#tree' => TRUE,
    '#weight' => -8,
    '#type' => 'container',
    '#id' => 'editor-settings-wrapper',
  );

  // Add editor-specific validation and submit handlers.
  if ($editor) {
    $plugin = $manager
      ->createInstance($editor->editor);
    $settings_form = array();
    $settings_form['#element_validate'][] = array(
      $plugin,
      'settingsFormValidate',
    );
    $form['editor']['settings']['subform'] = $plugin
      ->settingsForm($settings_form, $form_state, $editor);
    $form['editor']['settings']['subform']['#parents'] = array(
      'editor',
      'settings',
    );
    $form['#submit'][] = array(
      $plugin,
      'settingsFormSubmit',
    );
  }
  $form['#validate'][] = 'editor_form_filter_admin_format_validate';
  $form['#submit'][] = 'editor_form_filter_admin_format_submit';
}

/**
 * Button submit handler for filter_admin_format_form()'s 'editor_configure' button.
 */
function editor_form_filter_admin_format_editor_configure($form, &$form_state) {
  $editor = $form_state['editor'];
  if (isset($form_state['values']['editor']['editor'])) {
    if ($form_state['values']['editor']['editor'] === '') {
      $form_state['editor'] = FALSE;
    }
    elseif (empty($editor) || $form_state['values']['editor']['editor'] !== $editor->editor) {
      $editor = entity_create('editor', array(
        'format' => $form['#format']->format,
        'editor' => $form_state['values']['editor']['editor'],
      ));
      $form_state['editor'] = $editor;
    }
  }
  $form_state['rebuild'] = TRUE;
}

/**
 * AJAX callback handler for filter_admin_format_form().
 */
function editor_form_filter_admin_form_ajax($form, &$form_state) {
  return $form['editor']['settings'];
}

/**
 * Additional validate handler for filter_admin_format_form().
 */
function editor_form_filter_admin_format_validate($form, &$form_state) {

  // This validate handler is not applicable when using the 'Configure' button.
  if ($form_state['triggering_element']['#name'] === 'editor_configure') {
    return;
  }

  // When using this form with JavaScript disabled in the browser, the the
  // 'Configure' button won't be clicked automatically. So, when the user has
  // selected a text editor and has then clicked 'Save configuration', we should
  // point out that the user must still configure the text editor.
  if ($form_state['values']['editor']['editor'] !== '' && $form_state['editor'] === FALSE) {
    form_set_error('editor][editor', t('You must configure the selected text editor.'));
  }
}

/**
 * Additional submit handler for filter_admin_format_form().
 */
function editor_form_filter_admin_format_submit($form, &$form_state) {

  // Delete the existing editor if disabling or switching between editors.
  $format_id = $form['#format']->format;
  $original_editor = editor_load($format_id);
  if ($original_editor && $original_editor->editor != $form_state['values']['editor']) {
    $original_editor
      ->delete();
  }

  // Create a new editor or update the existing editor.
  if ($form_state['editor'] !== FALSE) {

    // Ensure the text format is set: when creating a new text format, this
    // would equal the empty string.
    $form_state['editor']->format = $form['#format']->format;
    $form_state['editor']->settings = $form_state['values']['editor']['settings'];
    $form_state['editor']
      ->save();
  }
}

/**
 * Loads an individual configured text editor based on text format ID.
 *
 * @return \Drupal\editor\Plugin\Core\Entity\Editor|FALSE
 *   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 #pre_render callback for 'text_format' elements.
 */
function editor_pre_render_format($element) {

  // Allow modules to programmatically enforce no client-side editor by setting
  // the #editor property to FALSE.
  if (isset($element['#editor']) && !$element['#editor']) {
    return $element;
  }

  // filter_process_format() copies properties to the expanded 'value' child
  // element. Skip this text format widget, if it contains no 'format' or when
  // the current user does not have access to edit the value.
  if (!isset($element['format']) || !empty($element['value']['#disabled'])) {
    return $element;
  }
  $format_ids = array_keys($element['format']['format']['#options']);

  // Early-return if no text editor is associated with any of the text formats.
  if (count(entity_load_multiple('editor', $format_ids)) === 0) {
    return $element;
  }

  // Use a hidden element for a single text format.
  $field_id = $element['value']['#id'];
  if (!$element['format']['format']['#access']) {

    // Use the first (and only) available text format.
    $format_id = $format_ids[0];
    $element['format']['editor'] = array(
      '#type' => 'hidden',
      '#name' => $element['format']['format']['#name'],
      '#value' => $format_id,
      '#attributes' => array(
        'class' => array(
          'editor',
        ),
        'data-editor-for' => $field_id,
      ),
    );
  }
  else {
    $element['format']['format']['#attributes']['class'][] = 'editor';
    $element['format']['format']['#attributes']['data-editor-for'] = $field_id;
  }

  // Attach Text Editor module's (this module) library.
  $element['#attached']['library'][] = array(
    'editor',
    'drupal.editor',
  );

  // Attach attachments for all available editors.
  $manager = drupal_container()
    ->get('plugin.manager.editor');
  $element['#attached'] = NestedArray::mergeDeep($element['#attached'], $manager
    ->getAttachments($format_ids));
  return $element;
}

Functions

Namesort descending Description
editor_element_info Implements hook_element_info().
editor_form_filter_admin_format_editor_configure Button submit handler for filter_admin_format_form()'s 'editor_configure' button.
editor_form_filter_admin_format_form_alter Implements hook_form_FORM_ID_alter().
editor_form_filter_admin_format_submit Additional submit handler for filter_admin_format_form().
editor_form_filter_admin_format_validate Additional validate handler for filter_admin_format_form().
editor_form_filter_admin_form_ajax AJAX callback handler for filter_admin_format_form().
editor_form_filter_admin_overview_alter Implements hook_form_FORM_ID_alter().
editor_help Implements hook_help().
editor_library_info Implements hook_library_info().
editor_load Loads an individual configured text editor based on text format ID.
editor_menu Implements hook_menu().
editor_menu_alter Implements hook_menu_alter().
editor_pre_render_format Additional #pre_render callback for 'text_format' elements.