You are here

class OrphanPurger in Entity Reference Revisions 8

Removes composite revisions that are no longer used.

Plugin annotation


@QueueWorker(
  id = "entity_reference_revisions_orphan_purger",
  title = @Translation("Entity Reference Revisions Orphan Purger"),
  cron = {"time" = 60}
)

Hierarchy

Expanded class hierarchy of OrphanPurger

File

src/Plugin/QueueWorker/OrphanPurger.php, line 21

Namespace

Drupal\entity_reference_revisions\Plugin\QueueWorker
View source
class OrphanPurger extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The purger.
   *
   * @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsOrphanPurger
   */
  protected $purger;

  /**
   * The database.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a new OrphanPurger instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\entity_reference_revisions\EntityReferenceRevisionsOrphanPurger $purger
   *   The purger service.
   * @param \Drupal\Core\Database\Connection $database
   *   The database service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
    $entity_type_id = $data['entity_type_id'];
    if (!$this->entityTypeManager
      ->hasDefinition($entity_type_id)) {
      return;
    }

    // Check the usage of data item and remove if not used.
    $composite_storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
    $composite_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $composite_revision_key = $composite_type
      ->getKey('revision');

    // Load all revisions of the composite type.
    // @todo Replace with an entity query on all revisions with a revision ID
    //   condition after https://www.drupal.org/project/drupal/issues/2766135.
    $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();

    /** @var \Drupal\Core\Entity\ContentEntityInterface $composite_revision */
    foreach ($composite_storage
      ->loadMultipleRevisions($entity_revision_ids) as $composite_revision) {
      if (!$this->purger
        ->isUsed($composite_revision)) {
        $this->purger
          ->deleteUnusedRevision($composite_revision);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrphanPurger::$database protected property The database.
OrphanPurger::$entityTypeManager protected property The entity type manager service.
OrphanPurger::$purger protected property The purger.
OrphanPurger::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
OrphanPurger::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
OrphanPurger::__construct public function Constructs a new OrphanPurger instance. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.