You are here

protected function MultiStepDisplay::executeJsCommand in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\MultiStepDisplay::executeJsCommand()

Execute command generated by front-end.

Parameters

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

1 call to MultiStepDisplay::executeJsCommand()
MultiStepDisplay::getForm in src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php
Returns selection display form.

File

src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php, line 186

Class

MultiStepDisplay
Show current selection and delivers selected entities.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay

Code

protected function executeJsCommand(FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  $commands = json_decode($triggering_element['#value'], TRUE);

  // Process Remove command.
  if (isset($commands['remove'])) {
    $entity_ids = $commands['remove'];

    // Remove weight of entity being removed.
    foreach ($entity_ids as $entity_info) {
      $entity_id_info = explode('_', $entity_info['entity_id']);
      $form_state
        ->unsetValue([
        'selected',
        $entity_info['entity_id'],
      ]);

      // Remove entity itself.
      $selected_entities =& $form_state
        ->get([
        'entity_browser',
        'selected_entities',
      ]);
      unset($selected_entities[$entity_id_info[2]]);
    }
    static::saveNewOrder($form_state);
  }

  // Process Add command.
  if (isset($commands['add'])) {
    $entity_ids = $commands['add'];
    $entities_to_add = [];
    $added_entities = [];

    // Generate list of entities grouped by type, to speed up loadMultiple.
    foreach ($entity_ids as $entity_pair_info) {
      $entity_info = explode(':', $entity_pair_info['entity_id']);
      if (!isset($entities_to_add[$entity_info[0]])) {
        $entities_to_add[$entity_info[0]] = [];
      }
      $entities_to_add[$entity_info[0]][] = $entity_info[1];
    }

    // Load Entities and add into $added_entities, so that we have list of
    // entities with key - "type:id".
    foreach ($entities_to_add as $entity_type => $entity_type_ids) {
      $indexed_entities = $this->entityTypeManager
        ->getStorage($entity_type)
        ->loadMultiple($entity_type_ids);
      foreach ($indexed_entities as $entity_id => $entity) {
        $added_entities[implode(':', [
          $entity_type,
          $entity_id,
        ])] = $entity;
      }
    }

    // Array is accessed as reference, so that changes are propagated.
    $selected_entities =& $form_state
      ->get([
      'entity_browser',
      'selected_entities',
    ]);

    // Fill list of selected entities in correct order with loaded entities.
    // In this case, order is preserved and multiple entities with same ID
    // can be selected properly.
    foreach ($entity_ids as $entity_pair_info) {
      $selected_entities[] = $added_entities[$entity_pair_info['entity_id']];
    }
  }
}