function batch_set in Drupal 8
Same name and namespace in other branches
- 6 includes/form.inc \batch_set()
- 7 includes/form.inc \batch_set()
- 9 core/includes/form.inc \batch_set()
Adds a new batch.
Batch operations are added as new batch sets. Batch sets are used to spread processing (primarily, but not exclusively, forms processing) over several page requests. This helps to ensure that the processing is not interrupted due to PHP timeouts, while users are still able to receive feedback on the progress of the ongoing operations. Combining related operations into distinct batch sets provides clean code independence for each batch set, ensuring that two or more batches, submitted independently, can be processed without mutual interference. Each batch set may specify its own set of operations and results, produce its own UI messages, and trigger its own 'finished' callback. Batch sets are processed sequentially, with the progress bar starting afresh for each new set.
Parameters
$batch_definition: An associative array defining the batch, with the following elements (all are optional except as noted):
- operations: (required) Array of operations to be performed, where each item is an array consisting of the name of an implementation of callback_batch_operation() and an array of parameter. Example:
array(
array(
'callback_batch_operation_1',
array(
$arg1,
),
),
array(
'callback_batch_operation_2',
array(
$arg2_1,
$arg2_2,
),
),
);
- title: A safe, translated string to use as the title for the progress page. Defaults to t('Processing').
- init_message: Message displayed while the processing is initialized. Defaults to t('Initializing.').
- progress_message: Message displayed while processing the batch. Available placeholders are @current, @remaining, @total, @percentage, @estimate and @elapsed. Defaults to t('Completed @current of @total.').
- error_message: Message displayed if an error occurred while processing the batch. Defaults to t('An error has occurred.').
- finished: Name of an implementation of callback_batch_finished(). This is executed after the batch has completed. This should be used to perform any result massaging that may be needed, and possibly save data in $_SESSION for display after final page redirection.
- file: Path to the file containing the definitions of the 'operations' and 'finished' functions, for instance if they don't reside in the main .module file. The path should be relative to base_path(), and thus should be built using drupal_get_path().
- library: An array of batch-specific CSS and JS libraries.
- url_options: options passed to the \Drupal\Core\Url object when constructing redirect URLs for the batch.
- progressive: 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 (default) indicates that the batch will finish in a single run.
- queue: An override of the default queue (with name and class fields
optional). An array containing two elements:
- name: Unique identifier for the queue.
- class: The name of a class that implements \Drupal\Core\Queue\QueueInterface, including the full namespace but not starting with a backslash. It must have a constructor with two arguments: $name and a \Drupal\Core\Database\Connection object. Typically, the class will either be \Drupal\Core\Queue\Batch or \Drupal\Core\Queue\BatchMemory. Defaults to Batch if progressive is TRUE, or to BatchMemory if progressive is FALSE.
Related topics
33 calls to batch_set()
- BatchTestChainedForm::batchTestChainedFormSubmit1 in core/
modules/ system/ tests/ modules/ batch_test/ src/ Form/ BatchTestChainedForm.php - Form submission handler #1 for batch_test_chained_form
- BatchTestChainedForm::batchTestChainedFormSubmit2 in core/
modules/ system/ tests/ modules/ batch_test/ src/ Form/ BatchTestChainedForm.php - Form submission handler #2 for batch_test_chained_form
- BatchTestChainedForm::batchTestChainedFormSubmit4 in core/
modules/ system/ tests/ modules/ batch_test/ src/ Form/ BatchTestChainedForm.php - Form submission handler #4 for batch_test_chained_form
- BatchTestController::testFinishRedirect in core/
modules/ system/ tests/ modules/ batch_test/ src/ Controller/ BatchTestController.php - Fires a batch process without a form submission and a finish redirect.
- BatchTestController::testLargePercentage in core/
modules/ system/ tests/ modules/ batch_test/ src/ Controller/ BatchTestController.php - Fires a batch process without a form submission.
File
- core/
includes/ form.inc, line 735 - Functions for form and batch generation and processing.
Code
function batch_set($batch_definition) {
if ($batch_definition) {
$batch =& batch_get();
// Initialize the batch if needed.
if (empty($batch)) {
$batch = [
'sets' => [],
'has_form_submits' => FALSE,
];
}
// Base and default properties for the batch set.
$init = [
'sandbox' => [],
'results' => [],
'success' => FALSE,
'start' => 0,
'elapsed' => 0,
];
$defaults = [
'title' => t('Processing'),
'init_message' => t('Initializing.'),
'progress_message' => t('Completed @current of @total.'),
'error_message' => t('An error has occurred.'),
];
$batch_set = $init + $batch_definition + $defaults;
// Tweak init_message to avoid the bottom of the page flickering down after
// init phase.
$batch_set['init_message'] .= '<br/> ';
// The non-concurrent workflow of batch execution allows us to save
// numberOfItems() queries by handling our own counter.
$batch_set['total'] = count($batch_set['operations']);
$batch_set['count'] = $batch_set['total'];
// Add the set to the batch.
if (empty($batch['id'])) {
// The batch is not running yet. Simply add the new set.
$batch['sets'][] = $batch_set;
}
else {
// The set is being added while the batch is running.
_batch_append_set($batch, $batch_set);
}
}
}