You are here

public function LibraryForm::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/LibraryForm.php, line 59
Contains Drupal\image_browser\Form.

Class

LibraryForm
Class TestModalForm.

Namespace

Drupal\image_browser\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $fid = 0;
  $userInput = $form_state
    ->getUserInput();
  if (isset($userInput['entity_browser_select'])) {
    foreach ($userInput['entity_browser_select'] as $file) {
      $fid = explode(':', $file)[1];
    }
  }
  if ($fid) {
    $file = File::load($fid);
    $file_url = file_create_url($file
      ->getFileUri());
    if ($file
      ->getMimeType() == 'image/svg+xml') {
      $preview = array(
        '#markup' => '<img src="' . $file_url . '"/>',
      );
    }
    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;
}