You are here

public function EditorImageDialog::submitForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/editor/src/Form/EditorImageDialog.php \Drupal\editor\Form\EditorImageDialog::submitForm()
  2. 10 core/modules/editor/src/Form/EditorImageDialog.php \Drupal\editor\Form\EditorImageDialog::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/editor/src/Form/EditorImageDialog.php, line 202

Class

EditorImageDialog
Provides an image dialog for text editors.

Namespace

Drupal\editor\Form

Code

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

  // Convert any uploaded files from the FID values to data-entity-uuid
  // attributes and set data-entity-type to 'file'.
  $fid = $form_state
    ->getValue([
    'fid',
    0,
  ]);
  if (!empty($fid)) {

    /** @var \Drupal\file\FileInterface $file */
    $file = $this->fileStorage
      ->load($fid);
    $file_url = $file
      ->createFileUrl();
    $form_state
      ->setValue([
      'attributes',
      'src',
    ], $file_url);
    $form_state
      ->setValue([
      'attributes',
      'data-entity-uuid',
    ], $file
      ->uuid());
    $form_state
      ->setValue([
      'attributes',
      'data-entity-type',
    ], 'file');
  }

  // 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',
    ], '');
  }
  if ($form_state
    ->getErrors()) {
    unset($form['#prefix'], $form['#suffix']);
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#editor-image-dialog-form', $form));
  }
  else {
    $response
      ->addCommand(new EditorDialogSave($form_state
      ->getValues()));
    $response
      ->addCommand(new CloseModalDialogCommand());
  }
  return $response;
}