Drupal 8 Batch Process

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 program.

According to Drupal.org batch operations can be defined as “...function allowing form processing spread out over several page requests, ensuring that the processing does not get interrupted because of a PHP timeout while also allowing the user to receive feedback on the progress of the ongoing operations.”.

In Drupal, without PHP, the timeout can be set using Batch API to perform programming operations smoothly.

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


name: batch_example

type: module

description: Example module for batch process

core: 8.x

package: Example


Let’s create routing.yml

batch_example.routing.yml

batch_example.delete_node_example:

  path: '/batch-example/delete'

  defaults:

    _form: '\Drupal\batch_example\Form\DeleteNodeForm'

    _title: 'Batch Example'

  requirements:

    _permission: 'access content'

src/Form/DeleteNodeForm.php

Here in the form we just need a submit button to handle the request.

<?php
namespace Drupal\batch_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class DeleteNodeForm.
*
* @package Drupal\batch_example\Form
*/
class DeleteNodeForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'delete_node_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['delete_node'] = array(
'#type' => 'submit',
'#value' => $this->t('Delete Node'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$nids = \Drupal::entityQuery('node')
->condition('type', 'article')
->sort('created', 'ASC')
->execute();
$batch = array(
'title' => t('Deleting Node...'),
'operations' => array(
array(
'\Drupal\batch_example\DeleteNode::deleteNodeExample',
array($nids)
),
),
'finished' => '\Drupal\batch_example\DeleteNode::deleteNodeExampleFinishedCallback',
);
batch_set($batch);
}
}


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

<?php
namespace Drupal\batch_example;
use Drupal\node\Entity\Node;
class DeleteNode {
public static function deleteNodeExample($nids, &$context){
$message = 'Deleting Node...';
$results = array();
foreach ($nids as $nid) {
$node = Node::load($nid);
$results[] = $node->delete();
}
$context['message'] = $message;
$context['results'] = $results;
}
function deleteNodeExampleFinishedCallback($success, $results, $operations) {
// The 'success' parameter means no fatal PHP errors were detected. All
// other error management should be handled using 'results'.
if ($success) {
$message = \Drupal::translation()->formatPlural(
count($results),
'One post processed.', '@count posts processed.'
);
}
else {
$message = t('Finished with an error.');
}
drupal_set_message($message);
}
}

This was all about creating batch process in Drupal 8

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...