function editor_form_filter_format_form_alter

Same name in other branches
  1. 9 core/modules/editor/editor.module \editor_form_filter_format_form_alter()
  2. 8.9.x core/modules/editor/editor.module \editor_form_filter_format_form_alter()
  3. 11.x core/modules/editor/editor.module \editor_form_filter_format_form_alter()

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

File

core/modules/editor/editor.module, line 97

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'] = [
        // 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'] = [
        '#type' => 'select',
        '#title' => t('Text editor'),
        '#options' => $editor_options,
        '#empty_option' => 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' => 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'] = 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.