| 6 form.inc | batch_set($batch_definition) |
| 7 form.inc | batch_set($batch_definition) |
| 8 form.inc | batch_set($batch_definition) |
Opens a new batch.
Parameters
$batch: An array defining the batch. The following keys can be used -- only 'operations' is required, and batch_init() provides default values for the messages.
- 'operations': Array of function calls to be performed. Example:
<?php
array(
array('my_function_1', array($arg1)),
array('my_function_2', array($arg2_1, $arg2_2)),
)
?>- 'title': Title for the progress page. Only safe strings should be passed. 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 a function to be 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().
Operations are added as new batch sets. Batch sets are used to ensure clean code independence, ensuring that several batches submitted by different parts of the code (core / contrib modules) can be processed correctly while not interfering or having to cope with each other. Each batch set gets to specify his own UI messages, operates on its own set of operations and results, and triggers its own 'finished' callback. Batch sets are processed sequentially, with the progress bar starting fresh for every new set.
Related topics
File
- includes/
form.inc, line 2445
Code
<?php
function batch_set($batch_definition) {
if ($batch_definition) {
$batch = &batch_get();
// Initialize the batch
if (empty($batch)) {
$batch = array(
'sets' => array(),
);
}
$init = array(
'sandbox' => array(),
'results' => array(),
'success' => FALSE,
);
// Use get_t() to allow batches at install time.
$t = get_t();
$defaults = array(
'title' => $t('Processing'),
'init_message' => $t('Initializing.'),
'progress_message' => $t('Remaining @remaining 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/> ';
$batch_set['total'] = count($batch_set['operations']);
// If the batch is being processed (meaning we are executing a stored submit handler),
// insert the new set after the current one.
if (isset($batch['current_set'])) {
// array_insert does not exist...
$slice1 = array_slice($batch['sets'], 0, $batch['current_set'] + 1);
$slice2 = array_slice($batch['sets'], $batch['current_set'] + 1);
$batch['sets'] = array_merge($slice1, array($batch_set), $slice2);
}
else {
$batch['sets'][] = $batch_set;
}
}
}
?> Login or register to post comments
Comments
The usual ways of quickly
The usual ways of quickly printing debug information - dpm(), var_dump(), drupal_set_message() - won't work very well inside of batch callback functions - there's no way to see what they're printing. Fortunately, there's another way that's not too difficult and not too hacky; PHP's error_log() function. It will print information to PHP's error log file (check php.ini or phpinfo() if you're not sure where that is, though you should).
You can keep an eye on the end of the log using
tail -f /path/to/php.login a terminal (or, if you're on a Mac, try /Applications/Utilities/Console). Then, in your code, useerror_log(print_r($var, TRUE));to print $var to the log. Run your batch, then switch back to your terminal/Console window and watch your code print out debug data in real time. Just remember to remove allerror_log()lines when you're done debugging, since they can slow down execution of your code.The lazy way to do this is
The lazy way to do this is use watchdog - watchdog('my batch action', var_dump($myvar, true)); - and then go look in reports.
Use Drush
Drush is great for this.
drush wd-show --tailReal-time logs streaming past in a console is way more convenient than hitting the reports page.
Devel's dd() function
Devel has a function dd() that works like dpm() but prints to a file instead of the screen.
It prints to a file called drupal_debug.txt that gets created in the directory you have set as drupal's temp file directory.