Skip to content
Snippets Groups Projects
Commit 5be93759 authored by Adrian Cid Almaguer's avatar Adrian Cid Almaguer
Browse files

Issue #3417950 by dharmendraqed42, chakkche, adriancid: Enhancement & Update...

Issue #3417950 by dharmendraqed42, chakkche, adriancid: Enhancement &  Update Drush command to allow specifying content types for deletion
parent 1f77665e
Branches 1.0.x
Tags 1.0.1
No related merge requests found
......@@ -3,7 +3,7 @@ services:
class: Drupal\node_revision_delete\Commands\QueueContent
arguments:
- '@entity_type.manager'
- '@plugin.manager.node_revision_delete'
- '@node_revision_delete'
- '@queue'
tags:
- { name: drush.command }
......
......@@ -5,7 +5,7 @@ namespace Drupal\node_revision_delete\Commands;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node_revision_delete\Plugin\NodeRevisionDeletePluginManager;
use Drupal\node_revision_delete\NodeRevisionDeleteInterface;
use Drush\Commands\DrushCommands;
/**
......@@ -23,11 +23,11 @@ class QueueContent extends DrushCommands {
protected EntityTypeManagerInterface $entityTypeManager;
/**
* Node Revision Plugin Manager.
* The NodeRevisionDelete service.
*
* @var \Drupal\node_revision_delete\Plugin\NodeRevisionDeletePluginManager
* @var \Drupal\node_revision_delete\NodeRevisionDeleteInterface
*/
protected NodeRevisionDeletePluginManager $pluginManager;
protected NodeRevisionDeleteInterface $nodeRevisionDelete;
/**
* The queue service.
......@@ -41,15 +41,15 @@ class QueueContent extends DrushCommands {
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\node_revision_delete\Plugin\NodeRevisionDeletePluginManager $node_revision_plugin_manager
* Node revision plugin manager.
* @param \Drupal\node_revision_delete\NodeRevisionDeleteInterface $node_revision_delete
* The node revision delete service.
* @param \Drupal\Core\Queue\QueueFactory $queue
* The queue service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, NodeRevisionDeletePluginManager $node_revision_plugin_manager, QueueFactory $queue) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, NodeRevisionDeleteInterface $node_revision_delete, QueueFactory $queue) {
parent::__construct();
$this->entityTypeManager = $entity_type_manager;
$this->pluginManager = $node_revision_plugin_manager;
$this->nodeRevisionDelete = $node_revision_delete;
$this->queue = $queue;
}
......@@ -59,28 +59,32 @@ class QueueContent extends DrushCommands {
* This creates queue items for all content which then can be processed
* during cron.
*
* @option type A comma-separated list of content types to process. If not provided, all content types will be processed.
*
* @usage drush node-revision-delete:queue
* Creates queue items for all content where settings apply.
* @usage drush node-revision-delete:queue --type=article,page
* Creates queue items for mentioned content types.
*
* @command node-revision-delete:queue
*
* @aliases nrd-q, nrd-queue
*/
public function queueContent(): void {
$content_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
public function queueContent($options = ['type' => '']): void {
$content_types = [];
if (!empty($options['type'])) {
$content_types = explode(',', $options['type']);
}
$content_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple($content_types);
foreach ($content_types as $content_type) {
$settings = $this->pluginManager->getSettingsNodeType($content_type->id());
// Check whether at least one plugin is enabled for this content type.
foreach ($settings['plugin'] as $plugin_settings) {
$status = (bool) ($plugin_settings['status'] ?? FALSE);
if ($status) {
// Create a queue for all nodes in this content type.
$this->createQueue($content_type->id());
break;
}
$has_enabled_plugins = $this->nodeRevisionDelete->contentTypeHasEnabledPlugins($content_type->id());
if ($has_enabled_plugins) {
// Create a queue for all nodes in this content type.
$this->createQueue($content_type->id());
}
}
}
/**
......@@ -98,11 +102,17 @@ class QueueContent extends DrushCommands {
->condition('type', $node_type)
->accessCheck(FALSE)
->execute();
$counter = 0;
foreach ($nids as $nid) {
$this->queue->get('node_revision_delete')->createItem($nid);
$nid = (int) $nid;
// Create queue item only if they dont exist.
if (!$this->nodeRevisionDelete->nodeExistsInQueue($nid)) {
$counter++;
$this->queue->get('node_revision_delete')->createItem($nid);
}
}
$this->output()->writeln(dt('<info>Created <comment>@count</comment> queue items for content-type: <comment>@node_type.</comment></info>', [
'@count' => count($nids),
'@count' => $counter,
'@node_type' => $node_type,
]));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment