You are here

public function BulkForm::viewsFormSubmit in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Plugin/views/field/BulkForm.php \Drupal\system\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/system/src/Plugin/views/field/BulkForm.php, line 350
Contains \Drupal\system\Plugin\views\field\BulkForm.

Class

BulkForm
Defines a actions-based bulk operation form element.

Namespace

Drupal\system\Plugin\views\field

Code

public function viewsFormSubmit(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->get('step') == 'views_form_views_form') {

    // Filter only selected checkboxes.
    $selected = array_filter($form_state
      ->getValue($this->options['id']));
    $entities = array();
    $action = $this->actions[$form_state
      ->getValue('action')];
    $count = 0;
    foreach ($selected as $bulk_form_key) {
      $entity = $this
        ->loadEntityFromBulkFormKey($bulk_form_key);

      // Skip execution if the user did not have access.
      if (!$action
        ->getPlugin()
        ->access($entity, $this->view
        ->getUser())) {
        $this
          ->drupalSetMessage($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(),
        ]), 'error');
        continue;
      }
      $count++;
      $entities[$bulk_form_key] = $entity;
    }
    $action
      ->execute($entities);
    $operation_definition = $action
      ->getPluginDefinition();
    if (!empty($operation_definition['confirm_form_route_name'])) {
      $options = array(
        'query' => $this
          ->getDestinationArray(),
      );
      $form_state
        ->setRedirect($operation_definition['confirm_form_route_name'], array(), $options);
    }
    else {

      // Don't display the message unless there are some elements affected and
      // there is no confirmation form.
      $count = count(array_filter($form_state
        ->getValue($this->options['id'])));
      if ($count) {
        drupal_set_message($this
          ->formatPlural($count, '%action was applied to @count item.', '%action was applied to @count items.', array(
          '%action' => $action
            ->label(),
        )));
      }
    }
  }
}