OrphanPurger.php in Entity Reference Revisions 8
File
src/Plugin/QueueWorker/OrphanPurger.php
View source
<?php
namespace Drupal\entity_reference_revisions\Plugin\QueueWorker;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\entity_reference_revisions\EntityReferenceRevisionsOrphanPurger;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrphanPurger extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $purger;
protected $database;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityReferenceRevisionsOrphanPurger $purger, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->purger = $purger;
$this->database = $database;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_reference_revisions.orphan_purger'), $container
->get('database'));
}
public function processItem($data) {
$entity_type_id = $data['entity_type_id'];
if (!$this->entityTypeManager
->hasDefinition($entity_type_id)) {
return;
}
$composite_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$composite_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$composite_revision_key = $composite_type
->getKey('revision');
$entity_revision_ids = $this->database
->select($composite_type
->getRevisionTable(), 'r')
->fields('r', [
$composite_revision_key,
])
->condition($composite_type
->getKey('id'), $data['entity_id'])
->orderBy($composite_revision_key)
->execute()
->fetchCol();
foreach ($composite_storage
->loadMultipleRevisions($entity_revision_ids) as $composite_revision) {
if (!$this->purger
->isUsed($composite_revision)) {
$this->purger
->deleteUnusedRevision($composite_revision);
}
}
}
}
Classes
Name |
Description |
OrphanPurger |
Removes composite revisions that are no longer used. |