function EditorHooks::formFilterFormatFormAlter

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

Attributes

#[Hook('form_filter_format_form_alter')]

File

core/modules/editor/src/Hook/EditorHooks.php, line 112

Class

EditorHooks
Hook implementations for editor.

Namespace

Drupal\editor\Hook

Code

public function formFilterFormatFormAlter(&$form, FormStateInterface $form_state) : void {
  $editor = $form_state->get('editor');
  if ($editor === NULL) {
    $format = $form_state->getFormObject()
      ->getEntity();
    $format_id = $format->isNew() ? NULL : $format->id();
    $editor = $format_id ? \Drupal::entityTypeManager()->getStorage('editor')
      ->load($format_id) : NULL;
    $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'] = [
    '#weight' => -9,
  ];
  $form['editor']['editor'] = [
    '#type' => 'select',
    '#title' => $this->t('Text editor'),
    '#options' => $editor_options,
    '#empty_option' => $this->t('None'),
    '#default_value' => $editor ? $editor->getEditor() : '',
    '#ajax' => [
      'trigger_as' => [
        'name' => 'editor_configure',
      ],
      'callback' => 'editor_form_filter_admin_form_ajax',
      'wrapper' => 'editor-settings-wrapper',
    ],
    '#weight' => -10,
  ];
  $form['editor']['configure'] = [
    '#type' => 'submit',
    '#name' => 'editor_configure',
    '#value' => $this->t('Configure'),
    '#limit_validation_errors' => [
      [
        'editor',
      ],
    ],
    '#submit' => [
      'editor_form_filter_admin_format_editor_configure',
    ],
    '#ajax' => [
      'callback' => 'editor_form_filter_admin_form_ajax',
      'wrapper' => 'editor-settings-wrapper',
    ],
    '#weight' => -10,
    '#attributes' => [
      'class' => [
        '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'] = $this->t('This option is disabled because no modules that provide a text editor are currently enabled.');
  }
  $form['editor']['settings'] = [
    '#tree' => TRUE,
    '#weight' => -8,
    '#type' => 'container',
    '#id' => 'editor-settings-wrapper',
  ];
  // Add editor-specific validation and submit handlers.
  if ($editor) {
    /** @var \Drupal\editor\Plugin\EditorPluginInterface $plugin */
    $plugin = $manager->createInstance($editor->getEditor());
    $form_state->set('editor_plugin', $plugin);
    $form['editor']['settings']['subform'] = [];
    $subform_state = SubformState::createForSubform($form['editor']['settings']['subform'], $form, $form_state);
    $form['editor']['settings']['subform'] = $plugin->buildConfigurationForm($form['editor']['settings']['subform'], $subform_state);
    $form['editor']['settings']['subform']['#parents'] = [
      'editor',
      'settings',
    ];
  }
  $form['#validate'][] = 'editor_form_filter_admin_format_validate';
  $form['actions']['submit']['#submit'][] = 'editor_form_filter_admin_format_submit';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.