function BatchTestCallbacks::callback2

Implements callback_batch_operation().

Performs a multistep batch operation.

File

core/modules/system/tests/modules/batch_test/src/BatchTestCallbacks.php, line 71

Class

BatchTestCallbacks
Batch callbacks for testing batches.

Namespace

Drupal\batch_test

Code

public function callback2($start, $total, $sleep, &$context) : void {
  $batch_test_helper = new BatchTestHelper();
  // Initialize context with progress information.
  if (!isset($context['sandbox']['current'])) {
    $context['sandbox']['current'] = $start;
    $context['sandbox']['count'] = 0;
  }
  // Process by groups of 5 (arbitrary value).
  $limit = 5;
  for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) {
    // No-op, but ensure the batch takes a couple iterations.
    // Batch needs time to run for the test, so sleep a bit.
    usleep($sleep);
    // Track execution, and store some result for post-processing in the
    // 'finished' callback.
    $id = $context['sandbox']['current'] + $i;
    $batch_test_helper->stack("op 2 id {$id}");
    $context['results'][2][] = $id;
    // Update progress information.
    $context['sandbox']['count']++;
  }
  $context['sandbox']['current'] += $i;
  // Inform batch engine about progress.
  if ($context['sandbox']['count'] != $total) {
    $context['finished'] = $context['sandbox']['count'] / $total;
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.