Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
namespace Drupal\Core\Batch;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Builds an array for a batch process.
*
* Example code to create a batch:
* @code
* $batch_builder = (new BatchBuilder())
* ->setTitle(t('Batch Title'))
* ->setFinishCallback('batch_example_finished_callback')
* ->setInitMessage(t('The initialization message (optional)'));
* foreach ($ids as $id) {
* $batch_builder->addOperation('batch_example_callback', [$id]);
* }
* batch_set($batch_builder->toArray());
* @endcode
*/
class BatchBuilder {
/**
* The set of operations to be processed.
*
* Each operation is a tuple of the function / method to use and an array
* containing any parameters to be passed.
*
* @var array
*/
protected $operations = [];
/**
* The title for the batch.
*
* @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
*/
protected $title;
/**
* The initializing message for the batch.
*
* @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
*/
protected $initMessage;
/**
* The message to be shown while the batch is in progress.
*
* @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
*/
protected $progressMessage;
/**
* The message to be shown if a problem occurs.
*
* @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
*/
protected $errorMessage;
/**
* The name of a function / method to be called when the batch finishes.
*
* @var string
*/
protected $finished;
/**
* The file containing the operation and finished callbacks.
*
* If the callbacks are in the .module file or can be autoloaded, for example,
* static methods on a class, then this does not need to be set.
*
* @var string
*/
protected $file;
/**
* An array of libraries to be included when processing the batch.
*
* @var string[]
*/
protected $libraries = [];
/**
* An array of options to be used with the redirect URL.
*
* @var array
*/
protected $urlOptions = [];
/**
* Specifies if the batch is progressive.
*
* If true, multiple calls are used. Otherwise an attempt is made to process
* the batch in a single run.
*
* @var bool
*/
protected $progressive = TRUE;
/**
* The details of the queue to use.
*
* A tuple containing the name of the queue and the class of the queue to use.
*
* @var array
*/
protected $queue;
/**
* Sets the default values for the batch builder.
*/
public function __construct() {
$this->title = new TranslatableMarkup('Processing');
$this->initMessage = new TranslatableMarkup('Initializing.');
$this->progressMessage = new TranslatableMarkup('Completed @current of @total.');
$this->errorMessage = new TranslatableMarkup('An error has occurred.');
}
/**
* Sets the title.
*
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $title
* The title.
*
* @return $this
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* Sets the finished callback.
*
* This callback will be executed if the batch process is done.
*
* @param callable $callback
* The callback.
*
* @return $this
*/
public function setFinishCallback(callable $callback) {
$this->finished = $callback;
return $this;
}
/**
* Sets the displayed message while processing is initialized.
*
* Defaults to 'Initializing.'.
*
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
* The text to display.
*
* @return $this
*/
public function setInitMessage($message) {
$this->initMessage = $message;
return $this;
}
/**
* Sets the message to display when the batch is being processed.
*
* Defaults to 'Completed @current of @total.'.
*
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
* The text to display. Available placeholders are:
* - '@current'
* - '@remaining'
* - '@total'
* - '@percentage'
* - '@estimate'
* - '@elapsed'.
*
* @return $this
*/
public function setProgressMessage($message) {
$this->progressMessage = $message;
return $this;
}
/**
* Sets the message to display if an error occurs while processing.
*
* Defaults to 'An error has occurred.'.
*
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
* The text to display.
*
* @return $this
*/
public function setErrorMessage($message) {
$this->errorMessage = $message;
return $this;
}
/**
* Sets the file that contains the callback functions.
*
* The path should be relative to base_path(), and thus should be built using
* drupal_get_path(). Defaults to {module_name}.module.
*
* @param string $filename
* The path to the file.
*
* @return $this
*/
public function setFile($filename) {
$this->file = $filename;
return $this;
}
/**
* Sets the libraries to use when processing the batch.
*
* Adds the libraries for use on the progress page. Any previously added
* libraries are removed.
*
* @param string[] $libraries
* The libraries to be used.
*
* @return $this
*/
public function setLibraries(array $libraries) {
$this->libraries = $libraries;
return $this;
}
/**
* Sets the options for redirect URLs.
*
* @param array $options
* The options to use.
*
* @return $this
*
* @see \Drupal\Core\Url
*/
public function setUrlOptions(array $options) {
$this->urlOptions = $options;
return $this;
}
/**
* Sets the batch to run progressively.
*
* @param bool $is_progressive
* (optional) A Boolean that indicates whether or not the batch needs to run
* progressively. TRUE indicates that the batch will run in more than one
* run. FALSE indicates that the batch will finish in a single run. Defaults
* to TRUE.
*
* @return $this
*/
public function setProgressive($is_progressive = TRUE) {
$this->progressive = $is_progressive;
return $this;
}
/**
* Sets an override for the default queue.
*
* The class will typically either be \Drupal\Core\Queue\Batch or
* \Drupal\Core\Queue\BatchMemory. The class defaults to Batch if progressive
* is TRUE, or to BatchMemory if progressive is FALSE.
*
* @param string $name
* The unique identifier for the queue.
* @param string $class
* The fully qualified name of a class that implements
* \Drupal\Core\Queue\QueueInterface.
*
* @return $this
*/
public function setQueue($name, $class) {
if (!class_exists($class)) {
throw new \InvalidArgumentException('Class ' . $class . ' does not exist.');
}
if (!in_array(QueueInterface::class, class_implements($class))) {
throw new \InvalidArgumentException(
'Class ' . $class . ' does not implement \Drupal\Core\Queue\QueueInterface.'
);
}
$this->queue = [
'name' => $name,
'class' => $class,
];
return $this;
}
/**
* Adds a batch operation.
*
* @param callable $callback
* The name of the callback function.
* @param array $arguments
* An array of arguments to pass to the callback function.
*
* @return $this
*/
public function addOperation(callable $callback, array $arguments = []) {
$this->operations[] = [$callback, $arguments];
return $this;
}
/**
* Converts a \Drupal\Core\Batch\Batch object into an array.
*
* @return array
* The array representation of the object.
*/
public function toArray() {
$array = [
'operations' => $this->operations ?: [],
'title' => $this->title ?: '',
'init_message' => $this->initMessage ?: '',
'progress_message' => $this->progressMessage ?: '',
'error_message' => $this->errorMessage ?: '',
'finished' => $this->finished,
'file' => $this->file,
'library' => $this->libraries ?: [],
'url_options' => $this->urlOptions ?: [],
'progressive' => $this->progressive,
];
if ($this->queue) {
$array['queue'] = $this->queue;
}
return $array;
}
}