You are here

public function UpdateUtils::getPreviousRevision in Scheduled Updates 8

Get the directly previous revision.

$entity->original will not ALWAYS be the previous revision.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity:

Return value

\Drupal\Core\Entity\EntityInterface|null

Overrides UpdateUtilsInterface::getPreviousRevision

File

src/UpdateUtils.php, line 201

Class

UpdateUtils
Service to determine information about Scheduled Update Types.

Namespace

Drupal\scheduled_updates

Code

public function getPreviousRevision(ContentEntityInterface $entity) {
  $storage = $this->entityTypeManager
    ->getStorage($entity
    ->getEntityTypeId());
  $query = $storage
    ->getQuery();
  $type = $entity
    ->getEntityType();
  $query
    ->allRevisions()
    ->condition($type
    ->getKey('id'), $entity
    ->id())
    ->condition($type
    ->getKey('revision'), $entity
    ->getRevisionId(), '<')
    ->sort($type
    ->getKey('revision'), 'DESC')
    ->pager(1);
  $revision_ids = $query
    ->execute();
  if ($revision_ids) {
    $revision_id = array_keys($revision_ids)[0];
    return $storage
      ->loadRevision($revision_id);
  }
  return NULL;
}