You are here

function batch_example_finished in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 batch_example/batch_example.module \batch_example_finished()
  2. 6 batch_example/batch_example.module \batch_example_finished()
  3. 7 batch_example/batch_example.module \batch_example_finished()

Batch 'finished' callback used by both batch 1 and batch 2.

Related topics

2 string references to 'batch_example_finished'
BatchExampleForm::generateBatch1 in modules/batch_example/src/Form/BatchExampleForm.php
Generate Batch 1.
BatchExampleForm::generateBatch2 in modules/batch_example/src/Form/BatchExampleForm.php
Generate Batch 2.

File

modules/batch_example/batch_example.module, line 109
Outlines how a module can use the Batch API.

Code

function batch_example_finished($success, $results, $operations) {
  $messenger = \Drupal::messenger();
  if ($success) {

    // Here we could do something meaningful with the results.
    // We just display the number of nodes we processed...
    $messenger
      ->addMessage(t('@count results processed.', [
      '@count' => count($results),
    ]));
    $messenger
      ->addMessage(t('The final result was "%final"', [
      '%final' => end($results),
    ]));
  }
  else {

    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    $messenger
      ->addMessage(t('An error occurred while processing @operation with arguments : @args', [
      '@operation' => $error_operation[0],
      '@args' => print_r($error_operation[0], TRUE),
    ]));
  }
}