Drupal batch processes in an update hook

When updating a module, it may be necessary to do something that can take a long time. You don't want to time out PHP, do you? Here's how to use Drupal's batch API in hook_update_N():

in mymodule.install

<?php
/**
 * Implements hook_update_N().
 * Runs 5 times.
 */
function mymodule_update_7001(&$sandbox) {
  $sandbox['total'] = 5;
  $sandbox['current'] = isset($sandbox['current']) ? $sandbox['current']++ : 1;
  $sandbox['finished'] = $sandbox['current'] / $sandbox['total'];
}
?>

Internal References

Article Type

General