Example: Batch API

Same name in other branches
  1. 3.x modules/batch_example/batch_example.module \batch_example
  2. 7.x-1.x batch_example/batch_example.module \batch_example

Outlines how a module can use the Batch API.

Batches allow heavy processing to be spread out over several page requests, ensuring that the processing does not get interrupted because of a PHP timeout, while allowing the user to receive feedback on the progress of the ongoing operations. It also can reduce out of memory situations.

The .install file also shows how the Batch API can be used to handle long-running hook_update_N() functions.

Two harmless batches are defined:

  • batch 1: Load the node with the lowest nid 100 times.
  • batch 2: Load all nodes, 20 times and uses a progressive op, loading nodes by groups of 5.

See also

Batch operations

Parent topics

File

modules/batch_example/batch_example.module, line 8

Functions

Title Sort descending File name Summary
batch_example_finished modules/batch_example/batch_example.module Batch 'finished' callback used by both batch 1 and batch 2.
batch_example_op_1 modules/batch_example/batch_example.module Batch operation for batch 1: one at a time.
batch_example_op_2 modules/batch_example/batch_example.module Batch operation for batch 2: five at a time.
batch_example_update_8001 modules/batch_example/batch_example.install Example of batch-driven update function.

Classes

Title Sort descending File name Summary
BatchExampleWebTest modules/batch_example/tests/src/Functional/BatchExampleWebTest.php Functional tests for the Batch Example module.