wartalab.blogspot.com is the best website and blog to provide tutorial for the, PHP, Drupal, Drupal 8 and general topic. Visit for programming knowledge and tutorial and solution
Drupal 8 : Entity Query and DB (Database) Query
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
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) { $...
-
For taxonomy term $ aliasManager = \Drupal::service(' path_alias.manager '); $ alias = $aliasManager->getAliasByPath(' /taxon...
-
Dependency injection is a technique whereby one object supplies the dependencies of another object What is Dependency Injection — Wikipe...
-
Drupal 10 builds upon the Drupal 9 but introduces several key changes and improvements. Below are the main differences: 1. Symfony Upgrade ...
-
The Drupal session management subsystem is built on top of the Symfony session component. It is optimized in order to minimize the impact o...
-
What are advantages of Drupal What are disadvantages of Drupal Difference between Drupal 7 and 8 How to connect multiple database to Drupal ...
-
Drupal 8 function drupal_mkdir drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) Creates a directory, optionally creatin...
-
Code that can be used to permanently delete unused/orphaned files in drupal 9 and 8. // get all files ids $fids = \Drupal:: entityQuery ...
-
A patch is a software update comprised code inserted (or patched) into the code of an executable program. Typically, a patch is installed in...
-
Drupal is not just a CMS. It's CMF (Content Management Framework). CMS is a derivative of CMF, a particular use case implemented by def...
-
Drupal 8 has introduced symfony request reponse object. So anything related to request/response header or body level changes should be usin...