You are here

protected function ViewsBulkOperationsBulkForm::updateTempstoreData in Views Bulk Operations (VBO) 8.2

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. 4.0.x 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.

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

File

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

Class

ViewsBulkOperationsBulkForm
Defines the Views Bulk Operations field plugin.

Namespace

Drupal\views_bulk_operations\Plugin\views\field

Code

protected function updateTempstoreData() {

  // 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(),
    'arguments' => $this->view->args,
    'redirect_url' => Url::createFromRequest(clone $this->requestStack
      ->getCurrentRequest()),
    'exposed_input' => $this->view
      ->getExposedInput(),
  ];
  $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 constant parameters.
    $this->tempStoreData += [
      'view_id' => $this->view
        ->id(),
      'display_id' => $this->view->current_display,
      'list' => [],
    ];

    // 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'] = [];
      }
      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);
    }
  }
}