Drupal Batch API in a Nutshell

Drupals internal batch API can be really helpful for handling large cumbersome processes on your web server. Rather than submitting a form and waiting for one of these processes to finish before reaching the next page, the batch API can be utilized to break the process down across multiple page loads. This not only cuts down on the server load, but will prevent the page from timing out. Progress bars will be displayed to the user while the process runs which will keep them informed of where they are at in the process.

In this example, I’ll explain how to do a simple assembly line-like process to generating page nodes in bulk, similar to Devel’s node generate function.

Youʼll need to start with a custom module. First, create a simple page with a form on it where you can specify the title of the pages, and how many you want to create. This is somewhat irrelevant but in order to show an example of how the batch API works we’ll need it.

[php]function batch_demo_menu(){
$items = array();
$items[‘batch-demo’] = array(
‘title’ => t(‘Batch API Demo’),
‘page callback’ => ‘drupal_get_form’,
‘page arguments’ => array(‘batch_demo_form’), ‘access arguments’ => array(‘access content’), ‘type’ => MENU_NORMAL_ITEM,
return $items;
function batch_demo_form(&$form_state){
$form = array();
$form[‘title’] = array(
‘#title’ => ‘Title’,
‘#type’ => ‘textfield’,
‘#required’ => TRUE,
‘#description’ => t(‘Enter the title of the pages.’),
$form[‘qty’] = array(
‘#title’ => ‘Quantity’, ‘#type’ => ‘textfield’, ‘#required’ => TRUE,
‘#default_value’ => 100,
‘#description’ => t(‘Enter the number of pages you want to create.’),
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Create’),
return $form;
function batch_demo_form_validate(&$form, &$form_state){
form_set_error(‘qty’,’You must enter a numeric value for quantity.’);

In the hook_form_submit() is where we will initialize the batch process.

[php]function batch_demo_form_submit(&$form, &$form_state){
‘title’ => t(‘Batch API Demo’), ‘operations’ => array(
// You can actually chain together multiple batch processes here
array(‘batch_demo_batch’, array($form_state[‘values’] [‘title’], $form_state[‘values’][‘qty’])), // Specify a callback for the batch
‘finished’ => ‘batch_demo_finished_batch’, // Finished batch callback.
// Start the batch process and tell it where to send the user when it’s done. In this case, we’ll send the user back to the batch-demo page.

Now we’ll setup the finished batch callback which will be used to run any final code at the end of the batch like displaying a message to the user.

[php]function register_parking_finished_batch($success, $results, $operations){
if ($success) {
$message = format_plural(count($results), ‘One node created’, ‘@count nodes created.’);
} else {
$message = t(‘Finished with an error.’);

Now for the meat and potatoes of the batch process.

[php]function batch_demo_batch($title, $qty, &$context){
global $user;
Set the number of nodes you want to create within a single iteration of the batch. This number
should be pretty low as it is the reason why we’re creating the batch process in the first place.
$limit = 10;

Set the variables that need to persist throughout the batch process. These need to be set inside
the $context array because that’s what persists throughout the process.
if (!isset($context[‘sandbox’][‘progress’])) {
$context[‘sandbox’][‘progress’] = 0; $context[‘sandbox’][‘title’] = $title; $context[‘sandbox’][‘max’] = $qty;

// Create some new nodes based on current progress and the $limit per page load. It’s important that you get this right otherwise you can potentially get stuck in an infinite loop or the process won’t do what you want it to.
for($i = 0; $i < $limit; $i++){
if($context[‘sandbox’][‘progress’] < $context[‘sandbox’][‘max’]){
$node = node_load();
$node->title = $context[‘sandbox’][‘title’].’ #’.$context [‘sandbox’][‘progress’];
$node->uid = $user->uid; $node->type = ‘page’;
$context[‘sandbox’][‘progress’]++; // Used to keep track of how many nodes we’ve already created.
$context[‘results’][] = check_plain($node->title); // Keep a running tab of all of the nodes created.
$context[‘message’] = "Created node " . $context [‘sandbox’][‘progress’] . " of " . $context[‘sandbox’][‘max’];
// Check if the batch is finished
if ($context[‘sandbox’][‘progress’] < $context[‘sandbox’] [‘max’]) {
$context[‘finished’] = $context[‘sandbox’][‘progress’] / $context[‘sandbox’][‘max’];

Download the example file.