Change record status: 
Project: 
Introduced in branch: 
11.x
Introduced in version: 
11.4.0
Description: 

What's changing?

  • Implemented the initial batch processor service
  • Deprecated related procedural Batch API functions

Batch Processor Instance

How to get the instance of the batch processor:

  • By using DI and autowire, set the type of the parameter in the class to \Drupal\Core\Batch\BatchProcessor
    For example:
      use \Drupal\Core\Batch\BatchProcessor;
      // ...
      public function __construct(
        protected readonly BatchProcessor $batchProcessor,
      ) {}
      
  • By using in a static or procedural context, use service name equal to \Drupal\Core\Batch\BatchProcessor::class
    For example:
      use \Drupal\Core\Batch\BatchProcessor;
      // ...
      $batch_processor = \Drupal::service(BatchProcessor::class);
      

Deprecated Public API

Here are the examples of how to replace the procedural public API with calls to the Batch Processor instance:

Deprecated Replacement
batch_set($batch_builder->toArray()); $batch_processor->queue($batch_builder->toArray());
batch_process(); $batch_processor->process();
$batch = &batch_get(); $batch = &$batch_processor->getCurrentBatch();

Deprecated Internal API

Note 1: The Internal Procedural Batch API is also BC compatible. Internal procedural functions will continue to work as expected until the Drupal 13.0.0 release.
Note 2: Follow the details about BC Policy regarding the usage of the Internal API

Here are the examples of how to replace the procedural internal API with calls to the Batch Processor instance:

Deprecated Replacement
_batch_needs_update(); $batch_processor->needsUpdate();
_batch_process(); $batch_processor->processQueue();
$set = &_batch_current_set(); $set = &$batch_processor->getCurrentSet();
_batch_next_set(); $batch_processor->nextSet();
_batch_finished(); $batch_processor->finishedProcessing();
_batch_shutdown(); $batch_processor->shutdown();
_batch_append_set($batch, $batch_set); $batch_processor->appendSet($batch, $batch_set);
_batch_queue($batch_set); $batch_processor->getQueue($batch_set);
Impacts: 
Module developers
Site templates, recipes and distribution developers