Batch process is the process of execution of a program in a series without interference of external source manually. In this tutorial, we will go through how to create a batch process in Drupal 8.  

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.

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.

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 Devel module to create dummy content.

 

 

comments powered by Disqus