5 * Batch callbacks for the Batch API tests.
8 use Drupal\Component\Utility\Html;
10 use Symfony\Component\HttpFoundation\RedirectResponse;
13 * Implements callback_batch_operation().
15 * Performs a simple batch operation.
17 function _batch_test_callback_1($id, $sleep, &$context) {
18 // No-op, but ensure the batch take a couple iterations.
19 // Batch needs time to run for the test, so sleep a bit.
21 // Track execution, and store some result for post-processing in the
22 // 'finished' callback.
23 batch_test_stack("op 1 id $id");
24 $context['results'][1][] = $id;
28 * Implements callback_batch_operation().
30 * Performs a multistep batch operation.
32 function _batch_test_callback_2($start, $total, $sleep, &$context) {
33 // Initialize context with progress information.
34 if (!isset($context['sandbox']['current'])) {
35 $context['sandbox']['current'] = $start;
36 $context['sandbox']['count'] = 0;
39 // Process by groups of 5 (arbitrary value).
41 for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) {
42 // No-op, but ensure the batch take a couple iterations.
43 // Batch needs time to run for the test, so sleep a bit.
45 // Track execution, and store some result for post-processing in the
46 // 'finished' callback.
47 $id = $context['sandbox']['current'] + $i;
48 batch_test_stack("op 2 id $id");
49 $context['results'][2][] = $id;
51 // Update progress information.
52 $context['sandbox']['count']++;
54 $context['sandbox']['current'] += $i;
56 // Inform batch engine about progress.
57 if ($context['sandbox']['count'] != $total) {
58 $context['finished'] = $context['sandbox']['count'] / $total;
63 * Implements callback_batch_operation().
65 function _batch_test_callback_5($id, $sleep, &$context) {
66 // No-op, but ensure the batch take a couple iterations.
67 // Batch needs time to run for the test, so sleep a bit.
69 // Track execution, and store some result for post-processing in the
70 // 'finished' callback.
71 batch_test_stack("op 5 id $id");
72 $context['results'][5][] = $id;
73 // This test is to test finished > 1
74 $context['finished'] = 3.14;
78 * Implements callback_batch_operation().
80 * Performs a batch operation setting up its own batch.
82 function _batch_test_nested_batch_callback() {
83 batch_test_stack('setting up batch 2');
84 batch_set(_batch_test_batch_2());
88 * Provides a common 'finished' callback for batches 1 to 4.
90 function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
92 foreach ($results as $op => $op_results) {
93 $messages[] = 'op ' . Html::escape($op) . ': processed ' . count($op_results) . ' elements';
101 // A fatal error occurred during the processing.
102 $error_operation = reset($operations);
103 $messages[] = t('An error occurred while processing @op with arguments:<br />@args', ['@op' => $error_operation[0], '@args' => print_r($error_operation[1], TRUE)]);
106 // Use item list template to render the messages.
108 '#type' => 'inline_template',
109 '#template' => 'results for batch {{ batch_id }}{{ errors }}',
111 'batch_id' => $batch_id,
113 '#theme' => 'item_list',
114 '#items' => $messages,
119 drupal_set_message(\Drupal::service('renderer')->renderPlain($error_message));
123 * Implements callback_batch_finished().
125 * Triggers 'finished' callback for batch 0.
127 function _batch_test_finished_0($success, $results, $operations) {
128 _batch_test_finished_helper(0, $success, $results, $operations);
132 * Implements callback_batch_finished().
134 * Triggers 'finished' callback for batch 1.
136 function _batch_test_finished_1($success, $results, $operations) {
137 _batch_test_finished_helper(1, $success, $results, $operations);
141 * Implements callback_batch_finished().
143 * Triggers 'finished' callback for batch 1.
145 function _batch_test_finished_1_finished($success, $results, $operations) {
146 _batch_test_finished_helper(1, $success, $results, $operations);
147 return new RedirectResponse(Url::fromRoute('test_page_test.test_page', [], ['absolute' => TRUE])->toString());
151 * Implements callback_batch_finished().
153 * Triggers 'finished' callback for batch 2.
155 function _batch_test_finished_2($success, $results, $operations) {
156 _batch_test_finished_helper(2, $success, $results, $operations);
160 * Implements callback_batch_finished().
162 * Triggers 'finished' callback for batch 3.
164 function _batch_test_finished_3($success, $results, $operations) {
165 _batch_test_finished_helper(3, $success, $results, $operations);
169 * Implements callback_batch_finished().
171 * Triggers 'finished' callback for batch 4.
173 function _batch_test_finished_4($success, $results, $operations) {
174 _batch_test_finished_helper(4, $success, $results, $operations);
178 * Implements callback_batch_finished().
180 * Triggers 'finished' callback for batch 5.
182 function _batch_test_finished_5($success, $results, $operations) {
183 _batch_test_finished_helper(5, $success, $results, $operations);