Use Drush command with Batch to update Node | TO THE NEW Blog
Summary
In this tutorial, we are going learn how to create/update nodes with the help of the Drush command using Batch API in Drupal. The file structure will be like below: Example: In the below example, we are updating the article content type Title field with a dummy value by dividing it into chunks of 100, similarly can add your own logic. try { $storage = $this->entityTypeManager->getStorage(node); $query = $storage->getQuery() ->condition(type, $type) ->condition(status, 1); $nidsCount = $query->execute(); $nidsCount = count($nidsCount); } catch (\Exception$e) { $this->output()->writeln($e); $this->loggerChannelFactory->get(module_name)->warning(Error found @e, [@e => $e]); } // 3. drush_backend_batch_process(); } } • Create a Helper File named UpdateNode.php in which we will write the articleUpdate and articleUpdateFinished function getStorage(node)->loadRevision($nid); $articleObject->set(title, update-title); $articleObject->save(); $context[results][] = $id; $context[message] = t(processing "@id" @response, [@id => $id, @response => $nid] ); } } /** * articleUpdateFinished callback. */ public function articleUpdateFinished($success, array $results, array $operations) { $messenger = \Drupal::messenger(); if ($success) { $messenger->addMessage(t(@count results processed., [@count => count($results)])); } } } Once the code is added to the module, enable the module or if you are using the existing module, then clear the drupal cache and run the drush update:node article command.