You are here

public function ViewsBulkOperationsBulkForm::viewsFormSubmit in Views Bulk Operations (VBO) 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsFormSubmit()
  2. 8.2 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::viewsFormSubmit()
  3. 4.0.x src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::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

src/Plugin/views/field/ViewsBulkOperationsBulkForm.php, line 600

Class

ViewsBulkOperationsBulkForm
Defines the Views Bulk Operations field plugin.

Namespace

Drupal\views_bulk_operations\Plugin\views\field

Code

public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->get('step') == 'views_form_views_form') {
    $action_id = $form_state
      ->getValue('action');
    $action = $this->actions[$action_id];
    $this->tempStoreData += [
      'action_id' => $action_id,
      'action_label' => empty($this->options['preconfiguration'][$action_id]['label_override']) ? (string) $action['label'] : $this->options['preconfiguration'][$action_id]['label_override'],
      'relationship_id' => $this->options['relationship'],
      'preconfiguration' => isset($this->options['preconfiguration'][$action_id]) ? $this->options['preconfiguration'][$action_id] : [],
      'list' => [],
      'view_id' => $this->view
        ->id(),
      'display_id' => $this->view->current_display,
      'batch' => $this->options['batch'],
      'arguments' => $this->view->args,
      'exposed_input' => $this->view
        ->getExposedInput(),
    ];

    // Get the current page for the "pass_view" functionality.
    if (!empty($action['pass_view'])) {
      $this->tempStoreData['current_page'] = 0;
      if (!empty($this->view->pager) && method_exists($this->view->pager, 'getCurrentPage')) {
        $this->tempStoreData['current_page'] = $this->view->pager
          ->getCurrentPage();
      }
    }
    if (!$form_state
      ->getValue('select_all')) {
      $selected = array_filter($form_state
        ->getValue($this->options['id']));
      $selected_indexes = [];
      foreach ($selected as $bulk_form_key) {
        $item = json_decode(base64_decode($bulk_form_key));
        $this->tempStoreData['list'][] = $item;
        $selected_indexes[] = $item[0];
      }

      // Filter selected entity labels.
      $this->tempStoreData['entity_labels'] = array_filter($this->tempStoreData['entity_labels'], function ($key) use ($selected_indexes) {
        return in_array($key, $selected_indexes, TRUE);
      }, ARRAY_FILTER_USE_KEY);
    }
    else {
      $this->tempStoreData['entity_labels'] = [];
    }
    $configurable = $this
      ->isConfigurable($action);

    // Get configuration if using AJAX.
    if ($configurable && empty($this->options['form_step'])) {
      $actionObject = $this->actionManager
        ->createInstance($action_id);
      if (method_exists($actionObject, 'submitConfigurationForm')) {
        $actionObject
          ->submitConfigurationForm($form, $form_state);
        $this->tempStoreData['configuration'] = $actionObject
          ->getConfiguration();
      }
      else {
        $form_state
          ->cleanValues();
        $this->tempStoreData['configuration'] = $form_state
          ->getValues();
      }
    }

    // Routing - determine redirect route.
    if ($this->options['form_step'] && $configurable) {
      $redirect_route = 'views_bulk_operations.execute_configurable';
    }
    elseif ($this->options['batch']) {
      if (!empty($action['confirm_form_route_name'])) {
        $redirect_route = $action['confirm_form_route_name'];
      }
      else {
        $redirect_route = 'views_bulk_operations.execute_batch';
      }
    }
    elseif (!empty($action['confirm_form_route_name'])) {
      $redirect_route = $action['confirm_form_route_name'];
    }

    // Redirect if needed.
    if (!empty($redirect_route)) {
      $this->tempStoreData['batch_size'] = $this->options['batch_size'];
      $this->tempStoreData['redirect_url'] = Url::createFromRequest(\Drupal::request());
      $this->userTempStore
        ->set($this->currentUser
        ->id(), $this->tempStoreData);
      $form_state
        ->setRedirect($redirect_route, [
        'view_id' => $this->view
          ->id(),
        'display_id' => $this->view->current_display,
      ]);
    }
    else {
      $this->actionProcessor
        ->executeProcessing($this->tempStoreData, $this->view);
    }
  }
}