You are here

function batch_example_batch_2 in Examples for Developers 7

Same name and namespace in other branches
  1. 6 batch_example/batch_example.module \batch_example_batch_2()

Batch 2 : Prepare a batch definition that will load all nodes 20 times.

Related topics

File

batch_example/batch_example.module, line 159
Outlines how a module can use the Batch API.

Code

function batch_example_batch_2() {
  $num_operations = 20;

  // Give helpful information about how many nodes are being operated on.
  $node_count = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')
    ->fetchField();
  drupal_set_message(t('There are @node_count nodes so each of the @num operations will require @count HTTP requests.', array(
    '@node_count' => $node_count,
    '@num' => $num_operations,
    '@count' => ceil($node_count / 5),
  )));
  $operations = array();

  // 20 operations, each one loads all nodes.
  for ($i = 0; $i < $num_operations; $i++) {
    $operations[] = array(
      'batch_example_op_2',
      array(
        t('(Operation @operation)', array(
          '@operation' => $i,
        )),
      ),
    );
  }
  $batch = array(
    'operations' => $operations,
    'finished' => 'batch_example_finished',
    // Message displayed while processing the batch. Available placeholders are:
    // @current, @remaining, @total, @percentage, @estimate and @elapsed.
    // These placeholders are replaced with actual values in _batch_process(),
    // using strtr() instead of t(). The values are determined based on the
    // number of operations in the 'operations' array (above), NOT by the number
    // of nodes that will be processed. In this example, there are 20
    // operations, so @total will always be 20, even though there are multiple
    // nodes per operation.
    // Defaults to t('Completed @current of @total.').
    'title' => t('Processing batch 2'),
    'init_message' => t('Batch 2 is starting.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Batch 2 has encountered an error.'),
  );
  return $batch;
}