You are here

function batch_example_batch_1 in Examples for Developers 7

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

Batch 1 definition: Load the node with the lowest nid 1000 times.

This creates an operations array defining what batch 1 should do, including what it should do when it's finished. In this case, each operation is the same and by chance even has the same $nid to operate on, but we could have a mix of different types of operations in the operations array.

Related topics

File

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

Code

function batch_example_batch_1() {
  $nid = batch_example_lowest_nid();
  $num_operations = 1000;
  drupal_set_message(t('Creating an array of @num operations', array(
    '@num' => $num_operations,
  )));
  $operations = array();

  // Set up an operations array with 1000 elements, each doing function
  // batch_example_op_1.
  // Each operation in the operations array means at least one new HTTP request,
  // running Drupal from scratch to accomplish the operation. If the operation
  // returns with $context['finished'] != TRUE, then it will be called again.
  // In this example, $context['finished'] is always TRUE.
  for ($i = 0; $i < $num_operations; $i++) {

    // Each operation is an array consisting of
    // - The function to call.
    // - An array of arguments to that function.
    $operations[] = array(
      'batch_example_op_1',
      array(
        $nid,
        t('(Operation @operation)', array(
          '@operation' => $i,
        )),
      ),
    );
  }
  $batch = array(
    'operations' => $operations,
    'finished' => 'batch_example_finished',
  );
  return $batch;
}