Drupal 8 and Drupal 9 custom Batch process

 Sometimes we need to run operations that will take a long time. We need to make sure the request doesn’t time out and it would be nice to let the user see that progress is being made. 

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.

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

batch_example.info.yml

***********************************************************

  1. name: Batch Example
  2. type: module
  3. description: 'Custom module for deleting all node'
  4. core_version_requirement: ^8.8 || ^9
***********************************************************

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);
  }

}
**************************************************************

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);
  }
}

****************************************************************


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)   {   $...