You are here

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

Same name and namespace in other branches
  1. 8.3 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 $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 186

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;
  }

  // Get view results if required.
  if (empty($list)) {
    $this->view
      ->setItemsPerPage($batch_size);
    $this->view
      ->setCurrentPage($current_batch);
    $this->view
      ->build();

    // If the view doesn't start from the first result,
    // move the offset.
    if ($view_offset = $this->view->pager
      ->getOffset()) {
      $offset += $view_offset;
    }
    $this->view->query
      ->setLimit($batch_size);
    $this->view->query
      ->setOffset($offset);

    // Let modules modify the view just prior to executing it.
    $this->moduleHandler
      ->invokeAll('views_pre_execute', [
      $this->view,
    ]);
    $this->view->query
      ->execute($this->view);

    // Prepare result getter.
    $this->viewDataService
      ->init($this->view, $this->view
      ->getDisplay(), $this->bulkFormData['relationship_id']);
    foreach ($this->view->result as $row) {
      $this->queue[] = $this->viewDataService
        ->getEntity($row);
    }
  }
  else {
    if ($batch_size) {
      $batch_list = array_slice($list, $offset, $batch_size);
    }
    else {
      $batch_list = $list;
    }
    foreach ($batch_list as $item) {
      $this->queue[] = $this
        ->getEntity($item);
    }
    if ($this->actionDefinition['pass_view']) {
      $this
        ->populateViewResult($batch_list, $context, $current_batch);
    }
  }

  // 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);
      }
    }
    if ($this->actionDefinition['pass_context']) {

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