Executing a pattern in Batch running mode
Last updated on
30 April 2025
When a pattern is executed in Batch running mode, the core function patterns_execute_pattern_batch() (includes/core/batch.inc) is called. In this mode, the actions are run out over several page requests. The following diagrams summarizes the process:

The workflow is very similar to the one explained in the previous section for the PHP running mode, but we will need to make use of the functions provided by the Batch API in order to offer feedback to the user:
-
The variable is decomposed in info, modules and actions sections.
-
The include options are retrieved.
-
Then we will loop through all the actions:
-
Reformat them using the function patterns_reformat_actions()
-
Set the $batch definition array. This batch is configured with a set of messages, and the values that are going to be used to display the progress: in our case the number of actions that are going to be executed
-
Then we set in $batch['operations'] all the functions that need to be executed. In our case, we will loop to add all the patterns_batch_action() necessary calls. Following our example (that only contains one operation), the $batch definition array will have the following values:
$batch Array [6] title Processing pattern <em class="placeholder">Example Article creation</em> progress_message Running section "<em class="placeholder">actions</em>" (<em class="placeholder">1</em>/<em class="placeholder">1</em>), action @current out of @total operations Array [1] 0 Array [2] 0 patterns_batch_action 1 Array [5] 0 create 1 Array [4] tag node type article title Test Article body lorem ipsum ... 2 0 3 Array [2] patterns Array [1] map <Uninitialized> 4 actions file sites/all/modules/custom/patterns/includes/core/batch.inc finished patterns_batch_finish init_message Initializing section "<em class="placeholder">actions</em>" (<em class="placeholder">1</em>/<em class="placeholder">1</em>)
-
- Finally we call batch_set() in order to notify Drupal we want to add these operations.
-
The batch will then perform a call for each of these operations, that in our case are always the function patterns_batch_action():
-
Call the same function that the PHP equivalent does to perform the prepare and validate phases: patterns_prepare_action()
-
Call the same function that the PHP equivalent does to perform the callbacks, build, params, execute and cleanup phases: patterns_implement_action()
-
-
Once all the operations have been performed, the hook patterns_batch_finish() is invoked allowing us to perform some extra processes once all the operations have been executed.
-
Display the successful execution messages.
-
Update the status of the metadata of the executed pattern (last execution time, change state to enable, etc.)
-
Clean caches
-
-
After all this process, the action has been successfully executed in Batch mode.
Help improve this page
Page status: Not set
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion