Sign in Registration
ArticlesDownloadServicesAudioContacts
ruen

Batch Operations in Drupal, Using the Batch API and Why It Doesn't Work

When implementing various functionality in Drupal, you may need to perform long operations. Anything can be such operations, for example, mass updating of data for materials, users, data import, etc. All of this is correctly called Drupal batch operations . A special API is provided for their correct execution.

batch-api-process

Performing long or large operations can be implemented in different ways. It is possible to write the code in such a way that everything will be done in one request, but here you need to take into account the PHP code execution time so that it does not exceed the established limits. Otherwise, the code will stop with an error and the operation will be aborted. You need to look at the parameters of a specific server, one may have problems, on the other everything will be fine.

The best option when developing Drupal projects should be using Batch . This technology allows you to split long operations into several small ones and execute each in a separate request, which removes restrictions on the PHP script execution time . Batch can also perform operations in one request - it independently distributes tasks and makes separate requests for them only if their execution time is more than 1 second.

How to use Batch in Drupal ? Batch is more related to the Form API, so it is optimal to call it from a Drupal form. But you can also from anywhere in the code, for this you need to add an additional line of code.

  function mymodule_myfunction () {
$ operations = array ();

// Selection of materials
$ result = db_select ('node', 'n') -> fields ('n', array ('nid')) -> execute ();

foreach ($ result as $ row) {
$ operations [] = array ('mymodule_change_date', array ($ row-> nid));
}

// Batch parameters
$ batch = array (
'operations' => $ operations,
'finished' => 'mymodule_batch_finished',
'title' => 'Performing an operation',
'init_message' => 'Please wait',
'progress_message' => 'Completed @current from @total',
'error_message' => 'An error occurred',
// If the functions are in another file, you must specify this, otherwise Batch does not work
'file' => drupal_get_path ('module', 'mymodule'). '/ mymodule.inc',
);

// Run Batch
batch_set ($ batch);

// It is necessary to write if the code is called not from the Form API
batch_process ();
}

function mymodule_change_date ($ nid) {
$ node = node_load ($ nid);
$ node-> created = time ();
node_save ($ node);
}

function mymodule_batch_finished ($ success, $ results, $ operations) {
if ($ success) {
drupal_set_message ('Operation completed successfully');
} else {
drupal_set_message ('Completed with errors', 'error');
}
}
 

As you can see, the code for running Batch in Drupal is quite simple. At the beginning, the data is prepared, for example, a selection of materials from the database is made and the array of operations is filled in in a loop. The name of the function and the parameter to which the nid is passed are specified. Next, the Batch itself is configured and launched using the batch_set function. Finally, the processing and completion functions are written.

But there are times when Batch does not work . If it is not called from the Form API, you must call the batch_process function, it will launch a progress bar. It is extremely important to remember that if the functions are in another file, you must specify this file using the file key, as shown in the example.

Thus, we have seen how to use batch operations in Drupal , namely using the Batch API. An example will help budding developers get started using this technology. If Batch does not work, please note the above possible causes.

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Communication

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to