You are here

function _batch_test_finished_helper in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc \_batch_test_finished_helper()
  2. 7 modules/simpletest/tests/batch_test.callbacks.inc \_batch_test_finished_helper()

Provides a common 'finished' callback for batches 1 to 7.

9 calls to _batch_test_finished_helper()
_batch_test_finished_0 in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc
Implements callback_batch_finished().
_batch_test_finished_1 in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc
Implements callback_batch_finished().
_batch_test_finished_1_finished in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc
Implements callback_batch_finished().
_batch_test_finished_2 in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc
Implements callback_batch_finished().
_batch_test_finished_3 in core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc
Implements callback_batch_finished().

... See full list

File

core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc, line 125
Batch callbacks for the Batch API tests.

Code

function _batch_test_finished_helper($batch_id, $success, $results, $operations, $elapsed) {
  if ($results) {
    foreach ($results as $op => $op_results) {
      $messages[] = 'op ' . Html::escape($op) . ': processed ' . count($op_results) . ' elements';
    }
  }
  else {
    $messages[] = 'none';
  }
  if (!$success) {

    // A fatal error occurred during the processing.
    $error_operation = reset($operations);
    $messages[] = t('An error occurred while processing @op with arguments:<br />@args', [
      '@op' => $error_operation[0],
      '@args' => print_r($error_operation[1], TRUE),
    ]);
  }

  // Use item list template to render the messages.
  $error_message = [
    '#type' => 'inline_template',
    '#template' => 'results for batch {{ batch_id }} ({{ elapsed }}){{ errors }}',
    '#context' => [
      'batch_id' => $batch_id,
      'elapsed' => $elapsed,
      'errors' => [
        '#theme' => 'item_list',
        '#items' => $messages,
      ],
    ],
  ];
  \Drupal::messenger()
    ->addStatus(\Drupal::service('renderer')
    ->renderPlain($error_message));
  \Drupal::messenger()
    ->addMessage('elapsed time: ' . $elapsed);
}