3 namespace Drupal\Core\Batch;
5 use Drupal\Core\Queue\QueueInterface;
6 use Drupal\Core\StringTranslation\TranslatableMarkup;
9 * Builds an array for a batch process.
11 * Example code to create a batch:
13 * $batch_builder = (new BatchBuilder())
14 * ->setTitle(t('Batch Title'))
15 * ->setFinishCallback('batch_example_finished_callback')
16 * ->setInitMessage(t('The initialization message (optional)'));
17 * foreach ($ids as $id) {
18 * $batch_builder->addOperation('batch_example_callback', [$id]);
20 * batch_set($batch_builder->toArray());
26 * The set of operations to be processed.
28 * Each operation is a tuple of the function / method to use and an array
29 * containing any parameters to be passed.
33 protected $operations = [];
36 * The title for the batch.
38 * @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
43 * The initializing message for the batch.
45 * @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
47 protected $initMessage;
50 * The message to be shown while the batch is in progress.
52 * @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
54 protected $progressMessage;
57 * The message to be shown if a problem occurs.
59 * @var string|\Drupal\Core\StringTranslation\TranslatableMarkup
61 protected $errorMessage;
64 * The name of a function / method to be called when the batch finishes.
71 * The file containing the operation and finished callbacks.
73 * If the callbacks are in the .module file or can be autoloaded, for example,
74 * static methods on a class, then this does not need to be set.
81 * An array of libraries to be included when processing the batch.
85 protected $libraries = [];
88 * An array of options to be used with the redirect URL.
92 protected $urlOptions = [];
95 * Specifies if the batch is progressive.
97 * If true, multiple calls are used. Otherwise an attempt is made to process
98 * the batch in a single run.
102 protected $progressive = TRUE;
105 * The details of the queue to use.
107 * A tuple containing the name of the queue and the class of the queue to use.
114 * Sets the default values for the batch builder.
116 public function __construct() {
117 $this->title = new TranslatableMarkup('Processing');
118 $this->initMessage = new TranslatableMarkup('Initializing.');
119 $this->progressMessage = new TranslatableMarkup('Completed @current of @total.');
120 $this->errorMessage = new TranslatableMarkup('An error has occurred.');
126 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $title
131 public function setTitle($title) {
132 $this->title = $title;
137 * Sets the finished callback.
139 * This callback will be executed if the batch process is done.
141 * @param callable $callback
146 public function setFinishCallback(callable $callback) {
147 $this->finished = $callback;
152 * Sets the displayed message while processing is initialized.
154 * Defaults to 'Initializing.'.
156 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
157 * The text to display.
161 public function setInitMessage($message) {
162 $this->initMessage = $message;
167 * Sets the message to display when the batch is being processed.
169 * Defaults to 'Completed @current of @total.'.
171 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
172 * The text to display. Available placeholders are:
182 public function setProgressMessage($message) {
183 $this->progressMessage = $message;
188 * Sets the message to display if an error occurs while processing.
190 * Defaults to 'An error has occurred.'.
192 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $message
193 * The text to display.
197 public function setErrorMessage($message) {
198 $this->errorMessage = $message;
203 * Sets the file that contains the callback functions.
205 * The path should be relative to base_path(), and thus should be built using
206 * drupal_get_path(). Defaults to {module_name}.module.
208 * @param string $filename
209 * The path to the file.
213 public function setFile($filename) {
214 $this->file = $filename;
219 * Sets the libraries to use when processing the batch.
221 * Adds the libraries for use on the progress page. Any previously added
222 * libraries are removed.
224 * @param string[] $libraries
225 * The libraries to be used.
229 public function setLibraries(array $libraries) {
230 $this->libraries = $libraries;
235 * Sets the options for redirect URLs.
237 * @param array $options
238 * The options to use.
242 * @see \Drupal\Core\Url
244 public function setUrlOptions(array $options) {
245 $this->urlOptions = $options;
250 * Sets the batch to run progressively.
252 * @param bool $is_progressive
253 * (optional) A Boolean that indicates whether or not the batch needs to run
254 * progressively. TRUE indicates that the batch will run in more than one
255 * run. FALSE indicates that the batch will finish in a single run. Defaults
260 public function setProgressive($is_progressive = TRUE) {
261 $this->progressive = $is_progressive;
266 * Sets an override for the default queue.
268 * The class will typically either be \Drupal\Core\Queue\Batch or
269 * \Drupal\Core\Queue\BatchMemory. The class defaults to Batch if progressive
270 * is TRUE, or to BatchMemory if progressive is FALSE.
272 * @param string $name
273 * The unique identifier for the queue.
274 * @param string $class
275 * The fully qualified name of a class that implements
276 * \Drupal\Core\Queue\QueueInterface.
280 public function setQueue($name, $class) {
281 if (!class_exists($class)) {
282 throw new \InvalidArgumentException('Class ' . $class . ' does not exist.');
285 if (!in_array(QueueInterface::class, class_implements($class))) {
286 throw new \InvalidArgumentException(
287 'Class ' . $class . ' does not implement \Drupal\Core\Queue\QueueInterface.'
299 * Adds a batch operation.
301 * @param callable $callback
302 * The name of the callback function.
303 * @param array $arguments
304 * An array of arguments to pass to the callback function.
308 public function addOperation(callable $callback, array $arguments = []) {
309 $this->operations[] = [$callback, $arguments];
314 * Converts a \Drupal\Core\Batch\Batch object into an array.
317 * The array representation of the object.
319 public function toArray() {
321 'operations' => $this->operations ?: [],
322 'title' => $this->title ?: '',
323 'init_message' => $this->initMessage ?: '',
324 'progress_message' => $this->progressMessage ?: '',
325 'error_message' => $this->errorMessage ?: '',
326 'finished' => $this->finished,
327 'file' => $this->file,
328 'library' => $this->libraries ?: [],
329 'url_options' => $this->urlOptions ?: [],
330 'progressive' => $this->progressive,
334 $array['queue'] = $this->queue;