Verified Commit f237db4b authored by Dave Long's avatar Dave Long
Browse files

refactor: #3568092 Convert editor.module procedural submit, validate and Ajax callbacks to methods

By: claudiu.cristea
By: dcam
By: nicxvan
parent 4bf14a08
Loading
Loading
Loading
Loading
Loading
+2 −1
Changes for core/modules/ckeditor5/ckeditor5.module: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -29,12 +29,13 @@ function ckeditor5_filter_format_edit_form_submit(array $form, FormStateInterfac
/**
 * AJAX callback handler for filter_format_form().
 *
 * Used instead of editor_form_filter_admin_form_ajax from the editor module.
 * Used instead of editor module's EditorHooks::editorFormFilterAdminFormAjax().
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\ckeditor5\Hook\Ckeditor5Hooks::updateCkeditor5HtmlFilter() instead.
 *
 * @see https://www.drupal.org/node/3566774
 * @see \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormAjax()
 */
function _update_ckeditor5_html_filter(array $form, FormStateInterface $form_state) {
  @trigger_error(__FUNCTION__ . 'is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal\ckeditor5\Hook\Ckeditor5Hooks::updateCkeditor5HtmlFilter() instead. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
+3 −1
Changes for core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -522,7 +522,7 @@ public function filterFormatEditFormSubmit(array $form, FormStateInterface $form
  /**
   * AJAX callback handler for filter_format_form().
   *
   * Used instead of editor_form_filter_admin_form_ajax from the editor module.
   * Used instead of EditorHooks::editorFormFilterAdminFormAjax().
   *
   * @param array $form
   *   The form render array.
@@ -531,6 +531,8 @@ public function filterFormatEditFormSubmit(array $form, FormStateInterface $form
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The Ajax response.
   *
   * @see \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormAjax()
   */
  public function updateCkeditor5HtmlFilter(array $form, FormStateInterface $form_state): AjaxResponse {
    $response = new AjaxResponse();
+4 −2
Changes for core/modules/ckeditor5/src/Plugin/Editor/CKEditor5.php: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
use Drupal\editor\Attribute\Editor;
use Drupal\editor\EditorInterface;
use Drupal\editor\Entity\Editor as EditorEntity;
use Drupal\editor\Hook\EditorHooks;
use Drupal\editor\Plugin\EditorBase;
use Drupal\filter\FilterFormatInterface;
use Psr\Log\LoggerInterface;
@@ -665,7 +666,7 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form

      // Special case: AJAX updates that do not submit the form (that cannot
      // result in configuration being saved).
      if (in_array('editor_form_filter_admin_format_editor_configure', $form_state->getSubmitHandlers(), TRUE)) {
      if (in_array(EditorHooks::class . ':editorFormFilterAdminFormatEditorConfigure', $form_state->getSubmitHandlers(), TRUE)) {
        // Ensure that plugins' validation constraints do not immediately
        // trigger a validation error: the user may choose to configure other
        // CKEditor 5 aspects first.
@@ -911,9 +912,10 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
    // @see ::validateConfigurationForm()
    $editor = $form_state->get('editor');

    // Prepare the editor settings for editor_form_filter_admin_format_submit().
    // Prepare settings for EditorHooks::editorFormFilterAdminFormatSubmit().
    // This strips away unwanted form values too, because those never can exist
    // in the already validated Editor config entity.
    // @see \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatSubmit()
    $form_state->setValues($editor->getSettings());

    parent::submitConfigurationForm($form, $form_state);
+34 −77
Changes for core/modules/editor/editor.module: 34 added lines, 77 removed lines.
Original line number Diff line number Diff line
@@ -5,112 +5,69 @@
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Form\SubformState;
use Drupal\editor\Entity\Editor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\editor\Entity\Editor;
use Drupal\editor\Hook\EditorHooks;
use Drupal\filter\FilterFormatInterface;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\text\Plugin\Field\FieldType\TextItemBase;

/**
 * Button submit handler for filter_format_form()'s 'editor_configure' button.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
 *   EditorHooks::editorFormFilterAdminFormatEditorConfigure() instead.
 *
 * @see https://www.drupal.org/node/3566774
 * @see \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatEditorConfigure()
 */
function editor_form_filter_admin_format_editor_configure($form, FormStateInterface $form_state): void {
  $editor = $form_state->get('editor');
  $editor_value = $form_state->getValue(['editor', 'editor']);
  if ($editor_value !== NULL) {
    if ($editor_value === '') {
      $form_state->set('editor', FALSE);
      $form_state->set('editor_plugin', NULL);
    }
    elseif (empty($editor) || $editor_value !== $editor->getEditor()) {
      $format = $form_state->getFormObject()->getEntity();
      $editor = Editor::create([
        'format' => $format->isNew() ? NULL : $format->id(),
        'editor' => $editor_value,
        'image_upload' => [
          'status' => FALSE,
        ],
      ]);
      $form_state->set('editor', $editor);
    }
  }
  $form_state->setRebuild();
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatEditorConfigure() instead. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  \Drupal::service(EditorHooks::class)->editorFormFilterAdminFormatEditorConfigure($form, $form_state);
}

/**
 * AJAX callback handler for filter_format_form().
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormAjax() instead.
 *
 * @see https://www.drupal.org/node/3566774
 */
function editor_form_filter_admin_form_ajax($form, FormStateInterface $form_state) {
  return $form['editor']['settings'];
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormAjax() instead. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  return \Drupal::service(EditorHooks::class)->editorFormFilterAdminFormAjax($form, $form_state);
}

/**
 * Additional validate handler for filter_format_form().
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatValidate()
 *   instead.
 *
 * @see https://www.drupal.org/node/3566774
 */
function editor_form_filter_admin_format_validate($form, FormStateInterface $form_state): void {
  $editor_set = $form_state->getValue(['editor', 'editor']) !== "";
  $subform_array_exists = (!empty($form['editor']['settings']['subform']) && is_array($form['editor']['settings']['subform']));
  if ($editor_set && $subform_array_exists && $editor_plugin = $form_state->get('editor_plugin')) {
    $subform_state = SubformState::createForSubform($form['editor']['settings']['subform'], $form, $form_state);
    $editor_plugin->validateConfigurationForm($form['editor']['settings']['subform'], $subform_state);
  }

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

  // When using this form with JavaScript disabled in the browser, 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->getValue(['editor', 'editor']) !== '' && !$form_state->get('editor')) {
    $form_state->setErrorByName('editor][editor', t('You must configure the selected text editor.'));
  }
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatValidate() instead. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  \Drupal::service(EditorHooks::class)->editorFormFilterAdminFormatValidate($form, $form_state);
}

/**
 * Additional submit handler for filter_format_form().
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatSubmit()
 *   instead.
 *
 * @see https://www.drupal.org/node/3566774
 */
function editor_form_filter_admin_format_submit($form, FormStateInterface $form_state): void {
  // Delete the existing editor if disabling or switching between editors.
  $format = $form_state->getFormObject()->getEntity();
  $format_id = $format->isNew() ? NULL : $format->id();
  $original_editor = $format_id ? Editor::load($format_id) : NULL;
  if ($original_editor && $original_editor->getEditor() != $form_state->getValue(['editor', 'editor'])) {
    $original_editor->delete();
  }

  $editor_set = $form_state->getValue(['editor', 'editor']) !== "";
  $subform_array_exists = (!empty($form['editor']['settings']['subform']) && is_array($form['editor']['settings']['subform']));
  if (($editor_plugin = $form_state->get('editor_plugin')) && $editor_set && $subform_array_exists) {
    $subform_state = SubformState::createForSubform($form['editor']['settings']['subform'], $form, $form_state);
    $editor_plugin->submitConfigurationForm($form['editor']['settings']['subform'], $subform_state);
  }

  // Create a new editor or update the existing editor.
  if ($editor = $form_state->get('editor')) {
    // Ensure the text format is set: when creating a new text format, this
    // would equal the empty string.
    $editor->set('format', $format_id);
    if ($settings = $form_state->getValue(['editor', 'settings'])) {
      $editor->setSettings($settings);
    }
    // When image uploads are disabled (status = FALSE), the schema for image
    // upload settings does not allow other keys to be present.
    // @see editor.image_upload_settings.*
    // @see editor.image_upload_settings.1
    // @see editor.schema.yml
    $image_upload_settings = $editor->getImageUploadSettings();
    if (!$image_upload_settings['status']) {
      $editor->setImageUploadSettings(['status' => FALSE]);
    }
    $editor->save();
  }
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use \Drupal\editor\Hook\EditorHooks::editorFormFilterAdminFormatSubmit() instead. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  \Drupal::service(EditorHooks::class)->editorFormFilterAdminFormatSubmit($form, $form_state);
}

/**
+123 −5
Changes for core/modules/editor/src/Hook/EditorHooks.php: 123 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -136,7 +136,7 @@ public function formFilterFormatFormAlter(&$form, FormStateInterface $form_state
        'trigger_as' => [
          'name' => 'editor_configure',
        ],
        'callback' => 'editor_form_filter_admin_form_ajax',
        'callback' => self::class . ':editorFormFilterAdminFormAjax',
        'wrapper' => 'editor-settings-wrapper',
      ],
      '#weight' => -10,
@@ -151,10 +151,10 @@ public function formFilterFormatFormAlter(&$form, FormStateInterface $form_state
              ],
      ],
      '#submit' => [
        'editor_form_filter_admin_format_editor_configure',
        self::class . ':editorFormFilterAdminFormatEditorConfigure',
      ],
      '#ajax' => [
        'callback' => 'editor_form_filter_admin_form_ajax',
        'callback' => self::class . ':editorFormFilterAdminFormAjax',
        'wrapper' => 'editor-settings-wrapper',
      ],
      '#weight' => -10,
@@ -185,8 +185,8 @@ public function formFilterFormatFormAlter(&$form, FormStateInterface $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';
    $form['#validate'][] = self::class . ':editorFormFilterAdminFormatValidate';
    $form['actions']['submit']['#submit'][] = self::class . ':editorFormFilterAdminFormatSubmit';
  }

  /**
@@ -485,4 +485,122 @@ protected function parseFileUuids(string $text): array {
    return $uuids;
  }

  /**
   * Submit handler for 'editor_configure' button from 'filter_format_form'.
   *
   * @param array $form
   *   The form render array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current form state.
   */
  public function editorFormFilterAdminFormatEditorConfigure(array $form, FormStateInterface $form_state): void {
    $editor = $form_state->get('editor');
    $editor_value = $form_state->getValue(['editor', 'editor']);
    if ($editor_value !== NULL) {
      if ($editor_value === '') {
        $form_state->set('editor', FALSE);
        $form_state->set('editor_plugin', NULL);
      }
      elseif (empty($editor) || $editor_value !== $editor->getEditor()) {
        $format = $form_state->getFormObject()->getEntity();
        $editor = Editor::create([
          'format' => $format->isNew() ? NULL : $format->id(),
          'editor' => $editor_value,
          'image_upload' => [
            'status' => FALSE,
          ],
        ]);
        $form_state->set('editor', $editor);
      }
    }
    $form_state->setRebuild();
  }

  /**
   * Ajax callback handler for 'filter_format_form' form.
   *
   * @param array $form
   *   The form render array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current form state.
   */
  public function editorFormFilterAdminFormAjax(array $form, FormStateInterface $form_state): array {
    return $form['editor']['settings'];
  }

  /**
   * Additional validate handler for 'filter_format_form' form.
   *
   * @param array $form
   *   The form render array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current form state.
   */
  public function editorFormFilterAdminFormatValidate(array $form, FormStateInterface $form_state): void {
    $editor_set = $form_state->getValue(['editor', 'editor']) !== "";
    $subform_array_exists = (!empty($form['editor']['settings']['subform']) && is_array($form['editor']['settings']['subform']));
    if ($editor_set && $subform_array_exists && $editor_plugin = $form_state->get('editor_plugin')) {
      $subform_state = SubformState::createForSubform($form['editor']['settings']['subform'], $form, $form_state);
      $editor_plugin->validateConfigurationForm($form['editor']['settings']['subform'], $subform_state);
    }

    // This validation handler should not be used with the 'Configure' button.
    if ($form_state->getTriggeringElement()['#name'] === 'editor_configure') {
      return;
    }

    // When using this form with JavaScript disabled in the browser, 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->getValue(['editor', 'editor']) !== '' && !$form_state->get('editor')) {
      $form_state->setErrorByName('editor][editor', $this->t('You must configure the selected text editor.'));
    }
  }

  /**
   * Additional submit handler for 'filter_format_form' form.
   *
   * @param array $form
   *   The form render array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current form state.
   */
  public function editorFormFilterAdminFormatSubmit(array $form, FormStateInterface $form_state): void {
    // Delete the existing editor if disabling or switching between editors.
    $format = $form_state->getFormObject()->getEntity();
    $format_id = $format->isNew() ? NULL : $format->id();
    $original_editor = $format_id ? Editor::load($format_id) : NULL;
    if ($original_editor && $original_editor->getEditor() != $form_state->getValue(['editor', 'editor'])) {
      $original_editor->delete();
    }

    $editor_set = $form_state->getValue(['editor', 'editor']) !== "";
    $subform_array_exists = (!empty($form['editor']['settings']['subform']) && is_array($form['editor']['settings']['subform']));
    if (($editor_plugin = $form_state->get('editor_plugin')) && $editor_set && $subform_array_exists) {
      $subform_state = SubformState::createForSubform($form['editor']['settings']['subform'], $form, $form_state);
      $editor_plugin->submitConfigurationForm($form['editor']['settings']['subform'], $subform_state);
    }

    // Create a new editor or update the existing editor.
    if ($editor = $form_state->get('editor')) {
      // Ensure the text format is set: when creating a new text format, this
      // would equal the empty string.
      $editor->set('format', $format_id);
      if ($settings = $form_state->getValue(['editor', 'settings'])) {
        $editor->setSettings($settings);
      }
      // When image uploads are disabled (status = FALSE), the schema for image
      // upload settings does not allow other keys to be present.
      // @see editor.image_upload_settings.*
      // @see editor.image_upload_settings.1
      // @see editor.schema.yml
      $image_upload_settings = $editor->getImageUploadSettings();
      if (!$image_upload_settings['status']) {
        $editor->setImageUploadSettings(['status' => FALSE]);
      }
      $editor->save();
    }
  }

}