eb1832185e8327f7b1f35888898efb605f187f97
[yaffs-website] / web / core / modules / system / tests / modules / batch_test / batch_test.callbacks.inc
1 <?php
2
3 /**
4  * @file
5  * Batch callbacks for the Batch API tests.
6  */
7
8 use Drupal\Component\Utility\Html;
9 use Drupal\Core\Url;
10 use Symfony\Component\HttpFoundation\RedirectResponse;
11
12 /**
13  * Implements callback_batch_operation().
14  *
15  * Performs a simple batch operation.
16  */
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.
20   usleep($sleep);
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;
25 }
26
27 /**
28  * Implements callback_batch_operation().
29  *
30  * Performs a multistep batch operation.
31  */
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;
37   }
38
39   // Process by groups of 5 (arbitrary value).
40   $limit = 5;
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.
44     usleep($sleep);
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;
50
51     // Update progress information.
52     $context['sandbox']['count']++;
53   }
54   $context['sandbox']['current'] += $i;
55
56   // Inform batch engine about progress.
57   if ($context['sandbox']['count'] != $total) {
58     $context['finished'] = $context['sandbox']['count'] / $total;
59   }
60 }
61
62 /**
63  * Implements callback_batch_operation().
64  */
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.
68   usleep($sleep);
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;
75 }
76
77 /**
78  * Implements callback_batch_operation().
79  *
80  * Performs a batch operation setting up its own batch.
81  */
82 function _batch_test_nested_batch_callback() {
83   batch_test_stack('setting up batch 2');
84   batch_set(_batch_test_batch_2());
85 }
86
87 /**
88  * Provides a common 'finished' callback for batches 1 to 4.
89  */
90 function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
91   if ($results) {
92     foreach ($results as $op => $op_results) {
93       $messages[] = 'op ' . Html::escape($op) . ': processed ' . count($op_results) . ' elements';
94     }
95   }
96   else {
97     $messages[] = 'none';
98   }
99
100   if (!$success) {
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)]);
104   }
105
106   // Use item list template to render the messages.
107   $error_message = [
108     '#type' => 'inline_template',
109     '#template' => 'results for batch {{ batch_id }}{{ errors }}',
110     '#context' => [
111       'batch_id' => $batch_id,
112       'errors' => [
113         '#theme' => 'item_list',
114         '#items' => $messages,
115       ],
116     ],
117   ];
118
119   \Drupal::messenger()->addStatus(\Drupal::service('renderer')->renderPlain($error_message));
120 }
121
122 /**
123  * Implements callback_batch_finished().
124  *
125  * Triggers 'finished' callback for batch 0.
126  */
127 function _batch_test_finished_0($success, $results, $operations) {
128   _batch_test_finished_helper(0, $success, $results, $operations);
129 }
130
131 /**
132  * Implements callback_batch_finished().
133  *
134  * Triggers 'finished' callback for batch 1.
135  */
136 function _batch_test_finished_1($success, $results, $operations) {
137   _batch_test_finished_helper(1, $success, $results, $operations);
138 }
139
140 /**
141  * Implements callback_batch_finished().
142  *
143  * Triggers 'finished' callback for batch 1.
144  */
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());
148 }
149
150 /**
151  * Implements callback_batch_finished().
152  *
153  * Triggers 'finished' callback for batch 2.
154  */
155 function _batch_test_finished_2($success, $results, $operations) {
156   _batch_test_finished_helper(2, $success, $results, $operations);
157 }
158
159 /**
160  * Implements callback_batch_finished().
161  *
162  * Triggers 'finished' callback for batch 3.
163  */
164 function _batch_test_finished_3($success, $results, $operations) {
165   _batch_test_finished_helper(3, $success, $results, $operations);
166 }
167
168 /**
169  * Implements callback_batch_finished().
170  *
171  * Triggers 'finished' callback for batch 4.
172  */
173 function _batch_test_finished_4($success, $results, $operations) {
174   _batch_test_finished_helper(4, $success, $results, $operations);
175 }
176
177 /**
178  * Implements callback_batch_finished().
179  *
180  * Triggers 'finished' callback for batch 5.
181  */
182 function _batch_test_finished_5($success, $results, $operations) {
183   _batch_test_finished_helper(5, $success, $results, $operations);
184 }