You are here

public static function DropzoneJsEbWidget::handleAjaxCommand in DropzoneJS 8

Same name and namespace in other branches
  1. 8.2 modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php \Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\DropzoneJsEbWidget::handleAjaxCommand()

Handling of automated submit of uploaded files.

Parameters

array $form: Form.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

Return value

\Drupal\Core\Ajax\AjaxResponse|array Returns ajax commands that will be executed in front-end.

File

modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php, line 532

Class

DropzoneJsEbWidget
Provides an Entity Browser widget that uploads new files.

Namespace

Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget

Code

public static function handleAjaxCommand(array $form, FormStateInterface $form_state) {

  // If there are some errors during submitting of form they should be
  // displayed, that's why we are returning status message here and generated
  // errors will be displayed properly in front-end.
  if (count($form_state
    ->getErrors()) > 0) {
    return [
      '#type' => 'status_messages',
    ];
  }

  // Output correct response if everything passed without any error.
  $ajax = new AjaxResponse();
  if (($triggering_element = $form_state
    ->getTriggeringElement()) && $triggering_element['#name'] === 'auto_select_handler') {
    $entity_ids = [];
    $added_entities = $form_state
      ->get([
      'dropzonejs',
      'added_entities',
    ]);

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    if (!empty($added_entities)) {
      foreach ($added_entities as $entity) {
        $entity_ids[] = $entity
          ->getEntityTypeId() . ':' . $entity
          ->id();
      }
    }

    // Add command to clear list of uploaded files. It's important to set
    // empty string value, in other case it will act as getter.
    $ajax
      ->addCommand(new InvokeCommand('[data-drupal-selector="edit-upload-uploaded-files"]', 'val', [
      '',
    ]));

    // Add Invoke command to select uploaded entities.
    $ajax
      ->addCommand(new InvokeCommand('.entities-list', 'trigger', [
      'add-entities',
      [
        $entity_ids,
      ],
    ]));
  }
  return $ajax;
}