Drupal Batch Example

Category: 

Drupal 7

Here's an example that utilizes the Batch API for Drupal 7:

/**
 * Implements hook_menu().
 */
function my_module_menu() {
  $items = array();
  $items['my_module/batch_fix'] = array(
    'title' => 'Batch fix',
    'page callback' => 'my_module_batch_fix',
    'access arguments' => array('administer users'),
  );
  return $items;
}

/**
 * The batch callback.
 */
function my_module_batch_fix() {
  $batch = array(
    'operations' => array(),
    'finished' => 'my_module_batch_fix_finished',
    'title' => t('Batch fix'),
    'init_message' => t('Fix is starting...'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Fix has encountered an error.')
  );
  $results = array("Hello", "World");
  foreach ($results as $result) {
    $batch['operations'][] = array('my_module_batch_fix_process', array($result));
  }
  batch_set($batch);
  batch_process('user'); // The path to redirect to when done.
}

/**
 * The batch processor.
 */
function my_module_batch_fix_process($word, &$context) {
  // Do heavy lifting here...
  // Display a progress message...
  $context['message'] = "Now processing $word...";
}

/**
 * The batch finish handler.
 */
function my_module_batch_fix_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message('Fix is complete!');
  }
  else {
    $error_operation = reset($operations);
    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array(
      '%error_operation' => $error_operation[0],
      '@arguments' => print_r($error_operation[1], TRUE)
    ));
    drupal_set_message($message, 'error');
  }
  drupal_set_message(l('Run again', 'my_module/batch_fix'));
}

Drupal 6

Here's an example that utilizes the Batch API for Drupal 6:

function example_menu() {
  $items = array();
  $items['example/batch'] = array(
    'title' => 'Example Batch',
    'page callback' => 'example_batch',
    'access arguments' => array('administer users')
  );
  return $items;
}

function example_batch() {
  $sql = " SELECT nid FROM {node} ORDER BY nid ASC LIMIT 5 ";
  $results = db_query($sql);
  $operations = array();
  while ($data = db_fetch_object($results)) {
    $operations[] = array('example_batch_process', array($data->nid));
  }
  $batch = array(
    'operations' => $operations,
    'finished' => 'example_batch_process_finished',
  );
  batch_set($batch);
  batch_process('user');
}

function example_batch_process($nid, &$context) {
  $node = node_load($nid);
  // Do some stuff here to make changes...
  $context['message'] = t('Processing @title', array('@title' => $node->title));
}

function example_batch_process_finished($success, $results, $operations) {
  if ($success) {
    $message = count($results) . ' processed.';
  }
  else {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
  }
  drupal_set_message($message);
  drupal_set_message(l('Run Batch Again', 'example/batch'));
}

Add new comment