You are here

public function UploadForm::submitForm in Image Entity Browser 8

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/UploadForm.php, line 64
Contains \Drupal\image_browser\Form.

Class

UploadForm
Class TestModalForm.

Namespace

Drupal\image_browser\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $fid = $form_state
    ->getValue(array(
    'image',
    0,
  ));
  if ($file = File::load($fid)) {
    $file_url = file_create_url($file
      ->getFileUri());
    if ($file
      ->getMimeType() == 'image/svg+xml') {
      $preview = array(
        '#markup' => '<img src="' . $file_url . '" style="max-width:100px"/>',
      );
    }
    else {
      $preview = array(
        '#theme' => 'image_style',
        '#style_name' => 'image_browser_thumbnail',
        '#uri' => $file
          ->getFileUri(),
      );
    }
    $response
      ->addCommand(new HtmlCommand('.image-browser.active .image-preview', $preview));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active input[type=hidden]', 'val', array(
      'file:' . $fid,
    )));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active', 'addClass', array(
      'has-image',
    )));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active input[type=hidden]', 'data', array(
      [
        'url' => $file_url,
      ],
    )));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active input[type=hidden]', 'trigger', array(
      'update',
    )));
  }
  else {
    $response
      ->addCommand(new HtmlCommand('.image-browser.active .image-preview', ''));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active input[type=hidden]', 'val', array(
      '',
    )));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active', 'removeClass', array(
      'has-image',
    )));
    $response
      ->addCommand(new InvokeCommand('.image-browser.active input[type=hidden]', 'trigger', array(
      'update',
    )));
  }
  $response
    ->addCommand(new CloseModalDialogCommand());
  return $response;
}