You are here

public function ViewsBulkOperationsActionProcessor::populateQueue in Views Bulk Operations (VBO) 8.3

Same name and namespace in other branches
  1. 8 src/Service/ViewsBulkOperationsActionProcessor.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor::populateQueue()
  2. 8.2 src/Service/ViewsBulkOperationsActionProcessor.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor::populateQueue()
  3. 4.0.x src/Service/ViewsBulkOperationsActionProcessor.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor::populateQueue()

Populate entity queue for processing.

Parameters

array $data: Data concerning the view that will be processed.

array $context: Batch API context.

Overrides ViewsBulkOperationsActionProcessorInterface::populateQueue

2 calls to ViewsBulkOperationsActionProcessor::populateQueue()
ViewsBulkOperationsActionProcessor::executeProcessing in src/Service/ViewsBulkOperationsActionProcessor.php
Helper function for processing results from view data.
ViewsBulkOperationsActionProcessor::getLabels in src/Service/ViewsBulkOperationsActionProcessor.php
Get the current processing entity queue.

File

src/Service/ViewsBulkOperationsActionProcessor.php, line 280

Class

ViewsBulkOperationsActionProcessor
Defines VBO action processor.

Namespace

Drupal\views_bulk_operations\Service

Code

public function populateQueue(array $data, array &$context = []) {
  $list = $data['list'];
  $base_field = $this->view->storage
    ->get('base_field');
  $this->queue = [];

  // Determine batch size and offset.
  if (!empty($context)) {
    $batch_size = $data['batch_size'];
    if (!isset($context['sandbox']['current_batch'])) {
      $context['sandbox']['current_batch'] = 0;
    }
    $current_batch =& $context['sandbox']['current_batch'];
    $offset = $current_batch * $batch_size;
  }
  else {
    $batch_size = 0;
    $current_batch = 0;
    $offset = 0;
  }
  if ($batch_size) {
    $batch_list = array_slice($list, $offset, $batch_size);
  }
  else {
    $batch_list = $list;
  }
  $this->view
    ->setItemsPerPage($batch_size);
  $this->view
    ->setCurrentPage(0);
  $this->view
    ->setOffset(0);
  $this->view
    ->initHandlers();
  $this->view
    ->setExposedInput([
    '_views_bulk_operations_override' => TRUE,
  ]);

  // Remove all exposed filters so we don't have any default filter
  // values that could make the actual selection out of range.
  if (!empty($this->view->filter)) {
    foreach ($this->view->filter as $id => $filter) {
      if (!empty($filter->options['exposed'])) {
        unset($this->view->filter[$id]);
      }
    }
  }

  // Build the view query.
  $this->view
    ->build();

  // Modify the view query: determine and apply the base field condition.
  $base_field_values = [];
  foreach ($batch_list as $item) {
    $base_field_values[] = $item[0];
  }
  if (empty($base_field_values)) {
    return 0;
  }
  if (isset($this->view->query->fields[$base_field])) {
    if (!empty($this->view->query->fields[$base_field]['table'])) {
      $base_field_alias = $this->view->query->fields[$base_field]['table'] . '.' . $this->view->query->fields[$base_field]['alias'];
    }
    else {
      $base_field_alias = $this->view->query->fields[$base_field]['alias'];
    }
  }
  else {
    $base_field_alias = $base_field;
  }
  $this->view->query
    ->addWhere(0, $base_field_alias, $base_field_values, 'IN');

  // Rebuild the view query.
  $this->view->query
    ->build($this->view);

  // We just destroyed any metadata that other modules may have added to the
  // query. Give those modules the opportunity to alter the query again.
  $this->view->query
    ->alter($this->view);

  // Execute the view.
  $this->moduleHandler
    ->invokeAll('views_pre_execute', [
    $this->view,
  ]);
  $this->view->query
    ->execute($this->view);

  // Get entities.
  $this->viewDataService
    ->init($this->view, $this->view
    ->getDisplay(), $this->bulkFormData['relationship_id']);
  foreach ($this->view->result as $row_index => $row) {

    // This may return rows for all possible languages.
    // Check if the current language is on the list.
    $found = FALSE;
    $entity = $this->viewDataService
      ->getEntity($row);
    foreach ($batch_list as $delta => $item) {
      if ($row->{$base_field} === $item[0] && $entity
        ->language()
        ->getId() === $item[1]) {
        $this->queue[] = $entity;
        $found = TRUE;
        unset($batch_list[$delta]);
        break;
      }
    }
    if (!$found) {
      unset($this->view->result[$row_index]);
    }
  }

  // Extra processing when executed in a Batch API operation.
  if (!empty($context)) {
    if (!isset($context['sandbox']['total'])) {
      if (empty($list)) {
        $context['sandbox']['total'] = $this->viewDataService
          ->getTotalResults($data['clear_on_exposed']);
      }
      else {
        $context['sandbox']['total'] = count($list);
      }
    }

    // Add batch size to context array for potential use in actions.
    $context['sandbox']['batch_size'] = $batch_size;
    $this
      ->setActionContext($context);
  }
  if ($batch_size) {
    $current_batch++;
  }
  $this
    ->setActionView();
  return count($this->queue);
}