You are here

public function EntityReferenceRevisionsOrphanPurger::isUsed in Entity Reference Revisions 8

Checks if the composite entity is used.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $composite_revision: The composite revision.

Return value

bool Whether the composite entity is used, FALSE if it is safe to delete.

1 call to EntityReferenceRevisionsOrphanPurger::isUsed()
EntityReferenceRevisionsOrphanPurger::deleteOrphansBatchOperation in src/EntityReferenceRevisionsOrphanPurger.php
Batch operation for checking orphans for a given entity type.

File

src/EntityReferenceRevisionsOrphanPurger.php, line 309

Class

EntityReferenceRevisionsOrphanPurger
Manages orphan composite revision deletion.

Namespace

Drupal\entity_reference_revisions

Code

public function isUsed(ContentEntityInterface $composite_revision) {
  $composite_type = $this->entityTypeManager
    ->getDefinition($composite_revision
    ->getEntityTypeId());
  $parent_type_field = $composite_type
    ->get('entity_revision_parent_type_field');
  $parent_type = $composite_revision
    ->get($parent_type_field)->value;
  $parent_field_name_field = $composite_type
    ->get('entity_revision_parent_field_name_field');
  $parent_field_name = $composite_revision
    ->get($parent_field_name_field)->value;
  $status = $this
    ->isValidParent($parent_type, $parent_field_name);
  if ($status !== static::PARENT_VALID) {
    return $status == static::PARENT_INVALID_SKIP ? TRUE : FALSE;
  }

  // Check if the revision is used in any revision of the parent, if that
  // entity type supports revisions.
  $query = $this->entityTypeManager
    ->getStorage($parent_type)
    ->getQuery()
    ->condition("{$parent_field_name}.target_revision_id", $composite_revision
    ->getRevisionId())
    ->range(0, 1)
    ->accessCheck(FALSE);
  if ($this->entityTypeManager
    ->getDefinition($parent_type)
    ->isRevisionable()) {
    $query = $query
      ->allRevisions();
  }
  $revisions = $query
    ->execute();

  // If there are parent revisions where this revision is used, skip it.
  return !empty($revisions);
}