You are here

protected function ViewsBulkOperationsBulkForm::updateTempstoreData in Views Bulk Operations (VBO) 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::updateTempstoreData()
  2. 8.2 src/Plugin/views/field/ViewsBulkOperationsBulkForm.php \Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm::updateTempstoreData()

Update tempstore data.

This function must be called a bit later, when the view query has been built. Also, no point doing this on the view admin page.

Parameters

array $bulk_form_keys: The calculated bulk form keys.

2 calls to ViewsBulkOperationsBulkForm::updateTempstoreData()
ViewsBulkOperationsBulkForm::viewsForm in src/Plugin/views/field/ViewsBulkOperationsBulkForm.php
Form constructor for the bulk form.
ViewsBulkOperationsBulkForm::viewsFormValidate in src/Plugin/views/field/ViewsBulkOperationsBulkForm.php

File

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

Class

ViewsBulkOperationsBulkForm
Defines the Views Bulk Operations field plugin.

Namespace

Drupal\views_bulk_operations\Plugin\views\field

Code

protected function updateTempstoreData(array $bulk_form_keys = NULL) {

  // Initialize tempstore object and get data if available.
  $this->tempStoreData = $this
    ->getTempstoreData($this->view
    ->id(), $this->view->current_display);

  // Parameters subject to change (either by an admin or user action).
  $variable = [
    'batch' => $this->options['batch'],
    'batch_size' => $this->options['batch'] ? $this->options['batch_size'] : 0,
    'total_results' => $this->viewData
      ->getTotalResults($this->options['clear_on_exposed']),
    'relationship_id' => $this->options['relationship'],
    'arguments' => $this->view->args,
    'exposed_input' => $this
      ->getExposedInput(),
  ];

  // Add bulk form keys when the form is displayed.
  if (isset($bulk_form_keys)) {
    $variable['bulk_form_keys'] = $bulk_form_keys;
  }

  // Set redirect URL taking destination into account.
  $request = $this->requestStack
    ->getCurrentRequest();
  $destination = $request->query
    ->get('destination');
  if ($destination) {
    $request->query
      ->remove('destination');
    unset($variable['exposed_input']['destination']);
    if (strpos($destination, '/') !== 0) {
      $destination = '/' . $destination;
    }
    $variable['redirect_url'] = Url::fromUserInput($destination, []);
  }
  else {
    $variable['redirect_url'] = Url::createFromRequest(clone $this->requestStack
      ->getCurrentRequest());
  }

  // Set exposed filters values to be kept after action execution.
  $query = $variable['redirect_url']
    ->getOption('query');
  if (!$query) {
    $query = [];
  }
  $query += $variable['exposed_input'];
  $variable['redirect_url']
    ->setOption('query', $query);

  // Create tempstore data object if it doesn't exist.
  if (!is_array($this->tempStoreData)) {
    $this->tempStoreData = [];

    // Add initial values.
    $this->tempStoreData += [
      'view_id' => $this->view
        ->id(),
      'display_id' => $this->view->current_display,
      'list' => [],
      'exclude_mode' => FALSE,
    ];

    // Add variable parameters.
    $this->tempStoreData += $variable;
    $this
      ->setTempstoreData($this->tempStoreData);
  }
  else {
    $update = FALSE;

    // Delete list if view arguments and optionally exposed filters changed.
    // NOTE: this should be subject to a discussion, maybe tempstore
    // should be arguments - specific?
    $clear_triggers = [
      'arguments',
    ];
    if ($this->options['clear_on_exposed']) {
      $clear_triggers[] = 'exposed_input';
    }
    foreach ($clear_triggers as $trigger) {
      if ($variable[$trigger] !== $this->tempStoreData[$trigger]) {
        $this->tempStoreData[$trigger] = $variable[$trigger];
        $this->tempStoreData['list'] = [];
        $this->tempStoreData['exclude_mode'] = FALSE;
      }
      unset($variable[$trigger]);
      $update = TRUE;
    }
    foreach ($variable as $param => $value) {
      if (!isset($this->tempStoreData[$param]) || $this->tempStoreData[$param] != $value) {
        $update = TRUE;
        $this->tempStoreData[$param] = $value;
      }
    }
    if ($update) {
      $this
        ->setTempstoreData($this->tempStoreData);
    }
  }
}