W3cubDocs

/Drupal 8

function editor_form_filter_format_form_alter

editor_form_filter_format_form_alter(&$form, FormStateInterface $form_state)

Implements hook_form_BASE_FORM_ID_alter() for \Drupal\filter\FilterFormatEditForm.

File

core/modules/editor/editor.module, line 93
Adds bindings for client-side "text editors" to text formats.

Code

function editor_form_filter_format_form_alter(&$form, FormStateInterface $form_state) {
  $editor = $form_state->get('editor');
  if ($editor === NULL) {
    $format = $form_state->getFormObject()->getEntity();
    $format_id = $format->isNew() ? NULL : $format->id();
    $editor = editor_load($format_id);
    $form_state->set('editor', $editor);
  }

  // Associate a text editor with this text format.
  $manager = \Drupal::service('plugin.manager.editor');
  $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->getEditor() : '',
    '#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',
    '#attached' => array(
      'library' => array(
        'editor/drupal.editor.admin',
      ),
    ),
  );

  // Add editor-specific validation and submit handlers.
  if ($editor) {
    $plugin = $manager->createInstance($editor->getEditor());
    $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['actions']['submit']['#submit'][] = array($plugin, 'settingsFormSubmit');
  }

  $form['#validate'][] = 'editor_form_filter_admin_format_validate';
  $form['actions']['submit']['#submit'][] = 'editor_form_filter_admin_format_submit';
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!editor!editor.module/function/editor_form_filter_format_form_alter/8.1.x