Using Batch API in hook_update_N in Drupal 7

February 27th, 2013

I just spent a lot of time figuring out why calling batch_process() would fail when I called it from a hook_update_N() function. I am using Drupal 7.

I wanted to insert terms in a vocabulary by using the taxonomy_csv API. This requires the following code.

module_load_include('inc','taxonomy_csv','import/taxonomy_csv.import.api');
    $options = _get_options();
    taxonomy_csv_import($options);

The taxonomy_csv_import function adds a batch job to Drupal. Here's what I learned. Batch jobs are run automatically in form submit functions and in the update.php script (which runs the hook_update_n() function). So no further action was needed.

However what caused an error for me was that I manually called batch_process() after taxonomy_import().

Hope this helps others.

Categories

IT

Tags