class BatchBuilder (View source)

Builds an array for a batch process.

Example code to create a batch:

Properties

protected array $operations

The set of operations to be processed.

protected string|TranslatableMarkup $title

The title for the batch.

protected string|TranslatableMarkup $initMessage

The initializing message for the batch.

protected string|TranslatableMarkup $progressMessage

The message to be shown while the batch is in progress.

protected string|TranslatableMarkup $errorMessage

The message to be shown if a problem occurs.

protected string $finished

The name of a function / method to be called when the batch finishes.

protected string $file

The file containing the operation and finished callbacks.

protected string[] $libraries

An array of libraries to be included when processing the batch.

protected array $urlOptions

An array of options to be used with the redirect URL.

protected bool $progressive

Specifies if the batch is progressive.

protected array $queue

The details of the queue to use.

Methods

__construct()

Sets the default values for the batch builder.

$this
setTitle(string|TranslatableMarkup $title)

Sets the title.

$this
setFinishCallback(callable $callback)

Sets the finished callback.

$this
setInitMessage(string|TranslatableMarkup $message)

Sets the displayed message while processing is initialized.

$this
setProgressMessage(string|TranslatableMarkup $message)

Sets the message to display when the batch is being processed.

$this
setErrorMessage(string|TranslatableMarkup $message)

Sets the message to display if an error occurs while processing.

$this
setFile(string $filename)

Sets the file that contains the callback functions.

$this
setLibraries(array $libraries)

Sets the libraries to use when processing the batch.

$this
setUrlOptions(array $options)

Sets the options for redirect URLs.

$this
setProgressive(bool $is_progressive = TRUE)

Sets the batch to run progressively.

$this
setQueue(string $name, string $class)

Sets an override for the default queue.

$this
addOperation(callable $callback, array $arguments = [])

Adds a batch operation.

array
toArray()

Converts a \Drupal\Core\Batch\Batch object into an array.

Details

__construct()

Sets the default values for the batch builder.

$this setTitle(string|TranslatableMarkup $title)

Sets the title.

Parameters

string|TranslatableMarkup $title

The title.

Return Value

$this

$this setFinishCallback(callable $callback)

Sets the finished callback.

This callback will be executed if the batch process is done.

Parameters

callable $callback

The callback.

Return Value

$this

$this setInitMessage(string|TranslatableMarkup $message)

Sets the displayed message while processing is initialized.

Defaults to 'Initializing.'.

Parameters

string|TranslatableMarkup $message

The text to display.

Return Value

$this

$this setProgressMessage(string|TranslatableMarkup $message)

Sets the message to display when the batch is being processed.

Defaults to 'Completed @current of @total.'.

Parameters

string|TranslatableMarkup $message

The text to display. Available placeholders are:

  • '@current'
  • '@remaining'
  • '@total'
  • '@percentage'
  • '@estimate'
  • '@elapsed'.

Return Value

$this

$this setErrorMessage(string|TranslatableMarkup $message)

Sets the message to display if an error occurs while processing.

Defaults to 'An error has occurred.'.

Parameters

string|TranslatableMarkup $message

The text to display.

Return Value

$this

$this setFile(string $filename)

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.

The file needs to be set before using ::addOperation(), ::setFinishCallback(), or any other function that uses callbacks from the file. This is so that PHP knows about the included functions.

Parameters

string $filename

The path to the file.

Return Value

$this

$this setLibraries(array $libraries)

Sets the libraries to use when processing the batch.

Adds the libraries for use on the progress page. Any previously added libraries are removed.

Parameters

array $libraries

The libraries to be used.

Return Value

$this

$this setUrlOptions(array $options)

Sets the options for redirect URLs.

Parameters

array $options

The options to use.

Return Value

$this

See also

Url

$this setProgressive(bool $is_progressive = TRUE)

Sets the batch to run progressively.

Parameters

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 Value

$this

$this setQueue(string $name, string $class)

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.

Parameters

string $name

The unique identifier for the queue.

string $class

The fully qualified name of a class that implements \Drupal\Core\Queue\QueueInterface.

Return Value

$this

$this addOperation(callable $callback, array $arguments = [])

Adds a batch operation.

Parameters

callable $callback

The name of the callback function.

array $arguments

An array of arguments to pass to the callback function.

Return Value

$this

array toArray()

Converts a \Drupal\Core\Batch\Batch object into an array.

Return Value

array

The array representation of the object.