You are here

public function EditorMediaDialog::submitForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/src/Form/EditorMediaDialog.php \Drupal\media\Form\EditorMediaDialog::submitForm()
  2. 9 core/modules/media/src/Form/EditorMediaDialog.php \Drupal\media\Form\EditorMediaDialog::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

core/modules/media/src/Form/EditorMediaDialog.php, line 233

Class

EditorMediaDialog
Provides a media embed dialog for text editors.

Namespace

Drupal\media\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();

  // When the `alt` attribute is set to two double quotes, transform it to the
  // empty string: two double quotes signify "empty alt attribute". See above.
  if (trim($form_state
    ->getValue([
    'attributes',
    'alt',
  ], '')) === '""') {
    $form_state
      ->setValue([
      'attributes',
      'alt',
    ], '""');
  }

  // The `alt` attribute is optional: if it isn't set, the default value
  // simply will not be overridden. It's important to set it to FALSE
  // instead of unsetting the value.  This way we explicitly inform
  // the client side about the new value.
  if ($form_state
    ->hasValue([
    'attributes',
    'alt',
  ]) && trim($form_state
    ->getValue([
    'attributes',
    'alt',
  ])) === '') {
    $form_state
      ->setValue([
      'attributes',
      'alt',
    ], FALSE);
  }

  // If the selected view mode matches the default on the filter, remove the
  // attribute.
  if (!empty($form_state
    ->get('filter_default_view_mode')) && $form_state
    ->getValue([
    'attributes',
    'data-view-mode',
  ]) === $form_state
    ->get('filter_default_view_mode')) {
    $form_state
      ->setValue([
      'attributes',
      'data-view-mode',
    ], FALSE);
  }
  if ($form_state
    ->getErrors()) {
    unset($form['#prefix'], $form['#suffix']);
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#editor-media-dialog-form', $form));
  }
  else {

    // Only send back the relevant values.
    $values = [
      'hasCaption' => $form_state
        ->getValue('hasCaption'),
      'attributes' => $form_state
        ->getValue('attributes'),
    ];
    $response
      ->addCommand(new EditorDialogSave($values));
    $response
      ->addCommand(new CloseModalDialogCommand());
  }
  return $response;
}