function BulkForm::viewsFormSubmit

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/field/BulkForm.php \Drupal\views\Plugin\views\field\BulkForm::viewsFormSubmit()
  2. 10 core/modules/views/src/Plugin/views/field/BulkForm.php \Drupal\views\Plugin\views\field\BulkForm::viewsFormSubmit()
  3. 11.x core/modules/views/src/Plugin/views/field/BulkForm.php \Drupal\views\Plugin\views\field\BulkForm::viewsFormSubmit()

Submit handler for the bulk form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Thrown when the user tried to access an action without access to it.

File

core/modules/views/src/Plugin/views/field/BulkForm.php, line 397

Class

BulkForm
Defines a actions-based bulk operation form element.

Namespace

Drupal\views\Plugin\views\field

Code

public function viewsFormSubmit(&$form, FormStateInterface $form_state) {
    if ($form_state->get('step') == 'views_form_views_form') {
        // Filter only selected checkboxes. Use the actual user input rather than
        // the raw form values array, since the site data may change before the
        // bulk form is submitted, which can lead to data loss.
        $user_input = $form_state->getUserInput();
        $selected = array_filter($user_input[$this->options['id']]);
        $entities = [];
        $action = $this->actions[$form_state->getValue('action')];
        $count = 0;
        foreach ($selected as $bulk_form_key) {
            $entity = $this->loadEntityFromBulkFormKey($bulk_form_key);
            // Skip execution if current entity does not exist.
            if (empty($entity)) {
                continue;
            }
            // Skip execution if the user did not have access.
            if (!$action->getPlugin()
                ->access($entity, $this->view
                ->getUser())) {
                $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[$bulk_form_key] = $entity;
        }
        // If there were entities selected but the action isn't allowed on any of
        // them, we don't need to do anything further.
        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 {
            // Don't display the message unless there are some elements affected and
            // there is no confirmation form.
            $this->messenger
                ->addStatus($this->formatPlural($count, '%action was applied to @count item.', '%action was applied to @count items.', [
                '%action' => $action->label(),
            ]));
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.