You are here

public function BulkFormEntityListBuilder::submitForm in Entity API 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/BulkFormEntityListBuilder.php, line 179

Class

BulkFormEntityListBuilder
Provides a list builder that allows using bulk actions.

Namespace

Drupal\entity

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $selected = array_filter($form_state
    ->getValue($this->entitiesKey));
  $entities = [];
  $action = $this->actions[$form_state
    ->getValue('action')];
  $count = 0;
  foreach ($selected as $id) {
    $entity = $this->entities[$id];

    // Skip execution if the user did not have access.
    if (!$action
      ->getPlugin()
      ->access($entity)) {
      $this
        ->messenger()
        ->addError($this
        ->t('No access to execute %action on the @entity_type_label %entity_label.', [
        '%action' => $action
          ->label(),
        '@entity_type_label' => $entity
          ->getEntityType()
          ->getLabel(),
        '%entity_label' => $entity
          ->label(),
      ]));
      continue;
    }
    $count++;
    $entities[$id] = $entity;
  }

  // Don't perform any action unless there are some elements affected.
  // @see https://www.drupal.org/project/drupal/issues/3018148
  if (!$count) {
    return;
  }
  $action
    ->execute($entities);
  $operation_definition = $action
    ->getPluginDefinition();
  if (!empty($operation_definition['confirm_form_route_name'])) {
    $options = [
      'query' => $this
        ->getDestinationArray(),
    ];
    $form_state
      ->setRedirect($operation_definition['confirm_form_route_name'], [], $options);
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->formatPlural($count, '%action was applied to @count item.', '%action was applied to @count items.', [
      '%action' => $action
        ->label(),
    ]));
  }
}