<About />

My name is Kevin. I am a web professional living in Massachusetts. I build websites mostly using Drupal and jQuery. I use Vim even when I don't need to. When I'm not on the computer, I'm usually hanging with my wife, Melissa.

<Search />

Using Drupal's Batch API

I don't know how I went so long without implementing Drupal's Batch API, but finally had a chance to play with it yesterday and had a Drupal Aha! moment. This particular API is very handy and surprisingly easy to implement.

We've been working on a way to synchronize a bunch of development environments using SVN and make it quick to deploy a new install of the site we're working on. The first step was to use the Import/Export module to dump content types, views and taxonomy definitions to text files. This way we could commit them to the repository and quickly build the framework for a fresh install of the site. Just install Drupal, then run the import function for the content types, views and taxonomies.

This was all great until we started exporting complex content types. It turned out that the system would fail if we tried to do more than one at a time, which meant going through the tedious effort of exporting... one... at... a... time.

Batch API to the rescue!

The Batch API module lets you easily set a batch of callbacks that Drupal will run one at a time, thus avoiding script timeouts for laborious tasks. The batch can be naturally set up in your form submit handler, or can be called in any other function by adding one extra line of code.

<?php
// Set up the Batch API
$batch = array('operations' => array(),
 
'finished' => '',
 
'title' => t('Exporting to files'),
 
'init_message' => t('Starting the export'),
 
'progress_message' => t('Exported @current out of @total'),
 
'error_message' => t('An error occurred and some or all of the exports have failed.'),
);
?>

Add as many functions to the batch queue as you need:

<?php
// Add an array declaring the callback function for your
// batch process and an array of callback arguments
$batch['operations'][] = array('your_batch_function', array($arg1, $arg2));
// add whatever logic you need
$batch['operations'][] = array('another_batch_function', array($arg1, $arg2));
?>

Tell the Batch API that you are finished:

<?php
if (!empty($batch['operations'])) {
 
batch_set($batch);
 
// batch_process() only needed if not inside a form _submit handler :
 
batch_process();
}
?>

drupal_execute() is not happy

One problem that I ran into along the way was the discovery that drupal_execute() has some compatibility issues with the Batch API. If I tried to use drupal_execute() in my batch callbacks, it would fail. Luckily there was a link to this tip to pause the batch that allowed the batch functions to use drupal_execute() with no complaints.

<?php
function your_batch_function($arg1, $arg2) {
 
// Pause the batch
 
$batch =& batch_get();
 
$ref = $batch;
 
$batch = null;

 
// run your code with drupal_execute()

  // Unpause the batch
 
$batch = $ref;
}
?>

Batching makes me happy

So, I just got done batch exporting 88 items including 19 super complex content types, 64 views and 4 vocabularies (one containing over 100 terms) and all it only took a few clicks. The Batch API is an amazing time saver. Combining it with some other awesome community tools like Import/Export gives me time to go take a much needed coffee break :)

:D

I got it :D

great blog thanks for shairng

great blog thanks for shairng

Thats a good tutorial on batch api

I am facing one problem related with batch api. I am using a customs function inside the batch function. And I am not getting any result. Any one can help me out on this.
Cheers!!

Cool

Drupal batch API is a really nice feature. Happy to see you put it to good use.

drupal_execute()

FYI the bug with drupal_execute() has been fixed and a patch has been committed. It is available in the -dev version of Drupal 6 now. Makes it even simpler!

Nice

That's fantastic... gotta love this community :)

what is it?

Sounds like a good timesaver, I really have to find an excuse to learn how to use it some time.

I'm just wondering though, what sort of system are you replicating that requires 64 views, just to get it off the ground??

:)

Ha, yeah that is a lot of views! Well, hopefully we'll be able to talk more about the actual site soon. It is a really complex site with a lot of moving pieces, though some of the views aren't entirely necessary. It was mostly cool to push the module really really hard and see what happened :)

If all goes well, we should be able to do a case study on it - very cool project!

Awesome!

That is a brilliant concept and a nice example.

Now I need to allocate some time to play with this... :)

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <a>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.