You are here

public static function ViewsBulkOperationsBatch::operation in Views Bulk Operations (VBO) 8.3

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

Batch operation callback.

Parameters

array $data: Processed view data.

array $context: Batch context.

3 calls to ViewsBulkOperationsBatch::operation()
drush_views_bulk_operations_execute in ./views_bulk_operations.drush.inc
The vbo-exec command execution function.
ViewsBulkOperationsCommands::vboExecute in src/Commands/ViewsBulkOperationsCommands.php
Execute an action on all results of the specified view.
ViewsBulkOperationsKernelTestBase::executeAction in tests/src/Kernel/ViewsBulkOperationsKernelTestBase.php
Execute an action on a specific view results.

File

src/ViewsBulkOperationsBatch.php, line 90

Class

ViewsBulkOperationsBatch
Defines module Batch API methods.

Namespace

Drupal\views_bulk_operations

Code

public static function operation(array $data, array &$context) {

  // Initialize batch.
  if (empty($context['sandbox'])) {
    $context['sandbox']['processed'] = 0;
    $context['results']['operations'] = [];
    $context['sandbox']['page'] = 0;
    $context['sandbox']['npages'] = ceil($data['total_results'] / $data['batch_size']);
  }

  // Get entities to process.
  $actionProcessor = \Drupal::service('views_bulk_operations.processor');
  $actionProcessor
    ->initialize($data);

  // Do the processing.
  $count = $actionProcessor
    ->populateQueue($data, $context);
  $batch_results = $actionProcessor
    ->process();
  if (!empty($batch_results)) {

    // Convert translatable markup to strings in order to allow
    // correct operation of array_count_values function.
    foreach ($batch_results as $result) {
      $context['results']['operations'][] = (string) $result;
    }
  }
  $context['sandbox']['processed'] += $count;
  $context['sandbox']['page']++;
  if ($context['sandbox']['page'] <= $context['sandbox']['npages']) {
    $context['finished'] = 0;
    $context['finished'] = $context['sandbox']['processed'] / $context['sandbox']['total'];
    $context['message'] = static::translate('Processed @count of @total entities.', [
      '@count' => $context['sandbox']['processed'],
      '@total' => $context['sandbox']['total'],
    ]);
  }
}