Skip to main content

Blog Post

Writing Object-Oriented Batch Processes

by Bobby Gryzynger
January 6, 2020

Drupal Core recently updated the approach for defining batch operations to a more modern Object-Oriented Programming (OOP) approach. Here's how to rewrite existing batch operation definitions using this new approach.

Writing batch process operations prior to Drupal 8.6 required developers to define an array for their batch processes along the lines of:

$batch = [
  'title' => t('My long-running process'),
  'operations' => [
  'finished' => 'my_finished_callback',

As these definitions grew more complex their readability began to suffer – so many arrays, so much indentation! Fret no more, because since Drupal 8.6 developers can define their batch operations using an extremely readable object-oriented format provided by Core's BatchBuilder class. 

Using BatchBuilder, our previous array-based format becomes:

use Drupal\Core\Batch\BatchBuilder;
$batch_builder = new BatchBuilder();
$batch_builder->setTitle(t('My long-running process'))
  ->addOperation('my_first_operation', ['parameter_1', 'parameter_2'])
  ->addOperation('my_second_operation', [])

Where creating batches used to require tedious coding of difficult to interpret arrays, the new BatchBuilder class allows developers to create batches using a clear, declarative format. The example shown above demonstrates a small set of the methods the BatchBuilder class provides – everything a developer previously defined in their batch arrays now has an analogous method in the BatchBuilder class.

One thing you may have noticed above is that, yes, batch_set() does not accept the BatchBuilder object, but the BatchBuilder class does provide a convenient method for converting the object to an array and BatchBuilder objects can be easily passed around your code like any other PHP object.

Further Reading

Meet team member, Bobby Gryzynger

Bobby is technologist with a strong background in art, media, and design creating his keen eye for Drupal Development. He enjoys expanding Drupal's reach by integrating external data into Drupal-based applications and has worked on huge projects including a faculty publications database at NYU whose data he integrated into a Drupal site.

He first started exploring web development as an undergraduate media production student looking to create a web presence for himself. He started with a simple, static HTML and CSS site and began adding more and more features. This sparked an interest in computer science and he began taking introductory programming and data structures courses. He continued studying computer science as a graduate art student, where he explored graphics and micro-controller programming while completing a software-based master's thesis project. After graduate school, he worked for a small NYC agency building static and CMS-based websites, but it wasn't until he took a job with NYU that he was introduced to Drupal. While at NYU, he managed over a dozen Drupal 7 and 8 sites and played a large role in developing custom modules and DevOps workflows. His desire to further contribute to the Drupal community and build his Drupal expertise led him to join Mediacurrent.

Outside of work, Bobby enjoys playing tennis and during his master's thesis, he took an interest in blowing glass and created 100 hand-blown, tungsten-filament light bulbs. He is also a new homeowner and enjoys working on home improvement projects including some landscaping, hardwood floor refinishing and wainscoting projects.

Learn more about Bobby >

Related Insights