Complex things made easy with Drupal’s Batch API

Jul
31

What gives your brain the energy to do complicated tasks and operate tons of information? Coffee, glucose, special memorizing techniques, a little workout, or perhaps the expectation of a nice vacation? Let it remain your secret — but we do know what helps websites. You could treat your Drupal site to coffee as well, but that’s not our topic today Instead, we’ll discuss a cool tool for running operations with large data and much more, called Batch API. Let’s take a look at its main principles, Drupal 7 and Drupal 8 versions, and main areas of use.

How Drupal’s Batch API works

Every big job is easier to do in small steps. Drupal’s Batch API breaks complex and time-consuming processes with large amounts of data into smaller segments or batches.

This approach lets your server “breathe more freely,” helps you avoid unpleasant PHP timeouts and out-of-memory cases. PHP’s maximum execution time is considered to be that of a small segment, not of the whole procedure, so you won’t reach the limit soon.

Another advantage of batches is that in the case of an error, it is much easier to go back to where it occurred, with no need to start from the beginning.

It is very important to keep the process under control and know how much has already been accomplished. So the Batch API offers a handy progress bar to inform developers, site admins or visitors about it, as well as show them any messages that may appear in the process.

Drupal Batch API in Drupal 7 and Drupal 8

No wonder this important assistant is included in Drupal core. Batch API in Drupal 8, as compared to Batch API in Drupal 7, has not changed in essence, though it now interacts with a few new core Drupal 8 objects.

The major improvement is that it is powered by the new controller, called BatchController, which takes care of the batch functions and returns minimal responses.

Some of Batch API use areas

Let’s look at a couple of examples of where Batch API can be very helpful to you.

With Batch API, it is possible to export large amounts of data from a Drupal website, even if you do not have access to the server, and when using Cron or Drush is impossible. You can get huge and customized reports, for example, in the CSV format.

Batch API will also assist you in processing large and complex files uploads, sending content to third-party APIs, and running processes that involve multiple users.

Batch API and hook_update_N()

An especially useful combination is Batch API with hook_update_N(). You can use it to update your module to a new version, programmatically update URL aliases, set default field values for a content type’s nodes and more.

If there is a risk of a PHP timeout, Batch API is your best choice. Use the $sandbox parameter to specify that you want Batch API to perform your operation, as well as make your settings.

Recap

Some operations just look complicated before you find the right approach. In this, the philosophy of Drupal’s Batch API and our support desk fully match. Contact us if you need any help making complex thing easier on your website — with Batch API or any other tools that will suit you best!

Recent Posts

27
Jul
The guide to implement a slideshow in Drupal 8

If you want to present your content in the form of a slideshow on your Drupal website, then the following information will be useful for you.

24
Jul
How to deliver your project on time and budget

IT technologies have become a significant constituent in a variety of industries, and specialists  increasingly face complex tasks of great importance.

About us and this blog

 
Request a free quote
Our friendly Help Team can be reached Monday through Friday, from 9am to 6pm, Eastern European Time Zone.

Contact now

Get new blog posts by email: