How to unpublish nodes using cron in drupal 8/9

lakshmi , Credit to  volkotech-solutions Jan 10

Cron is a daemon that runs commands at regular intervals. Cron is a time-based task scheduler that can be configured to automatically execute tasks after the initial configuration.

Drupal 8 introduces the concept of Service classes, which capture application-wide behaviours (such as database access, email sending, and text translation) in PHP classes rather than global functions.

For example, During a cron run, unpublish all nodes that are older than 30 days.

Implement a Custom Module:

Modules in Drupal are located in the /modules folder. We'll create two custom and contrib folders inside the /modules folder. Additional modules from drupal.org will be stored in the contrib folder, and our custom modules will be stored in the custom folder.

Create a .info.yml file:

Let's name our custom module lotus. Create the folder /modules/custom/lotus. In this folder, we'll need to create the lotus.info.yml file and add the following code.

name: Unpublished old nodes
description: old node unpublish when cron runs.
type: module
core_version_requirement: ^8 || ^9

Implement the service class:

In lotus/src/OldNodesService.php, implement the service class as follows:

<?php
namespace Drupal\lotus;
 
use Drupal\Core\Entity\EntityTypeManager;
 
class OldNodesService {
protected $entityTypeManager;
 
public function __construct(EntityTypeManager $entity_type_manager) {
  $this->entityTypeManager = $entity_type_manager;
}
 
public function load() {
  $storage = $this->entityTypeManager->getStorage('node');
  $query = $storage->getQuery()->condition('created',
    strtotime('-30 days'), '<');
  $nids = $query->execute();
  return $storage->loadMultiple($nids);
}
}

Register service:

In lotus/lotus.services.yml, register your service and describe its dependencies.

services:
lotus.old_nodes:
  class: \Drupal\lotus\OldNodesService
  arguments: ["@entity_type.manager"]

Implement hook_cron():

The hook_cron() should be implemented in the .module file. When the Drupal cron runs, the hook_cron() is triggered. As a result, we can define our task within this.

<?php
/**
* Implements hook_cron().
*/
function lotus_cron() {
 $old_nodes = \Drupal::service('lotus.old_nodes')->load();
 foreach ($old_nodes as $node) {
   $node->setPublished(false);
   $node->save();
 }
}

Enable a module:

Using the Manage administrative menu, navigate to the Extend page in the list of modules, search the Unpublished old nodes module and then select its checkbox. Scroll down to the bottom of the webpage, and then click Install.

Navigate to the /admin/config/system/cron then click “Run Cron”. Once cron run, unpublish all nodes that are older than 30 days.

Conclusion:

When the Drupal cron runs, the hook_cron() function is called. As a result, we can define our task within the context of this. That's all! You have successfully created a custom module for unpublish nodes using cron programmatically in Drupal 8/9.

 

Comments