You are here

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

Same name and namespace in other branches
  1. 8.3 src/Service/ViewsBulkOperationsActionProcessor.php \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor::populateQueue()
  2. 8 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 $list: Array of selected view results.

array $context: Batch API context.

Overrides ViewsBulkOperationsActionProcessorInterface::populateQueue

1 call to ViewsBulkOperationsActionProcessor::populateQueue()
ViewsBulkOperationsActionProcessor::executeProcessing in src/Service/ViewsBulkOperationsActionProcessor.php
Helper function for processing results from view data.

File

src/Service/ViewsBulkOperationsActionProcessor.php, line 207

Class

ViewsBulkOperationsActionProcessor
Defines VBO action processor.

Namespace

Drupal\views_bulk_operations\Service

Code

public function populateQueue(array $list, array &$context = []) {

  // Determine batch size and offset.
  if (!empty($context)) {
    $batch_size = $this->bulkFormData['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;
  }
  $base_field_values = [];
  foreach ($batch_list as $item) {
    $base_field_values[] = $item[0];
  }
  if (empty($base_field_values)) {
    return 0;
  }
  $this->view
    ->setItemsPerPage(0);
  $this->view
    ->setCurrentPage(0);
  $this->view
    ->setOffset(0);
  $this->view
    ->initHandlers();

  // 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 = $this->view->storage
    ->get('base_field');
  if (isset($this->view->query->fields[$base_field])) {
    $base_field_alias = $this->view->query->fields[$base_field]['table'] . '.' . $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);

  // 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();
      }
      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);
}