You are here

public function EditorFileDialog::submitForm in CkEditor Background Image 8

Same name and namespace in other branches
  1. 2.0.x src/Form/EditorFileDialog.php \Drupal\ckeditor_bgimage\Form\EditorFileDialog::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

src/Form/EditorFileDialog.php, line 183

Class

EditorFileDialog
Provides a link dialog for text editors.

Namespace

Drupal\ckeditor_bgimage\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $form_state
    ->setValue([
    'attributes',
    'idModal',
  ], rand(1000000, 99999999));
  $fid = $form_state
    ->getValue([
    'fid',
    0,
  ]);
  if (!empty($fid)) {

    /** @var \Drupal\file\FileInterface */
    $file = $this->fileStorage
      ->load($fid);
    $file_url = file_create_url($file
      ->getFileUri());
    $file_url = file_url_transform_relative($file_url);
    $form_state
      ->setValue([
      'attributes',
      'image',
    ], $file_url);
  }
  if ($form_state
    ->getErrors()) {
    unset($form['#prefix'], $form['#suffix']);
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#editor-bgimage-dialog-form', $form));
    return $response;
  }
  $response
    ->addCommand(new EditorDialogSave($form_state
    ->getValues()));
  $response
    ->addCommand(new CloseModalDialogCommand());
  return $response;
}