Batch process is the process of execution of a program in a series without interference of external source manually. In a Batch process, inputs are taken in batches or slots. However, now this has become more irrelevant with the term batch and essentially means to run a process or a job without an end user’s interaction. In Drupal, batch process will help to execute a similar programme. 


In this tutorial, we will go through how to create a drupal batch process.


For the process we need a controller or a form from which we can trigger the batch process. In our case we will create a batch process to delete all Node. 
So let’s create a small module for that.

Drupal module development

Module Setup:
batch_example.info.yml

Let’s create routing.yml
batch_example.routing.yml

src/Form/DeleteNodeForm.php
Here in the form we just need a submit button to handle the request.

Here in the submission handler where we have defined the batch. 
$batch is an array of steps to be performed in batch process. In this batch we have two steps,

  • deleteNodeExample : In this method we will be handling deletion of node.
  • deleteNodeExampleFinishedCallback: This is the finish call back it will be called once deletion of all node is done. It will give the status message of the process.
  • Batch_set : Drupal batch_set method will execute set of operations in made in batches, so as to not interrupt processing.  

Now we need to define these methods, So will create a class say “DeleteNode” inside src directory.

src/DeleteNode.php

This all about creating batch process in Drupal 8. For testing the batch we can use drupal  Devel module to create dummy content.

 

 

comments powered by Disqus