You are here

public function ScheduledTransitionsNewRevision::latestRevision in Scheduled Transitions 2.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/ScheduledTransitionsNewRevision.php \Drupal\scheduled_transitions\EventSubscriber\ScheduledTransitionsNewRevision::latestRevision()

Latest revision functionality.

Automatically determines the latest revision, which will be the new saved revision.

Parameters

\Drupal\scheduled_transitions\Event\ScheduledTransitionsNewRevisionEvent $event: New revision event.

File

src/EventSubscriber/ScheduledTransitionsNewRevision.php, line 104

Class

ScheduledTransitionsNewRevision
Determines which revision should be transitioned.

Namespace

Drupal\scheduled_transitions\EventSubscriber

Code

public function latestRevision(ScheduledTransitionsNewRevisionEvent $event) : void {
  $scheduledTransition = $event
    ->getScheduledTransition();
  $options = $scheduledTransition
    ->getOptions();
  if (!isset($options[ScheduledTransition::OPTION_LATEST_REVISION])) {
    return;
  }

  // Load the latest revision.
  $entity = $scheduledTransition
    ->getEntity();
  if ($entity) {

    /** @var \Drupal\Core\Entity\EntityStorageInterface|\Drupal\Core\Entity\RevisionableStorageInterface $entityStorage */
    $entityStorage = $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId());
    $latestRevisionId = $entityStorage
      ->getLatestRevisionId($entity
      ->id());
    if ($latestRevisionId) {

      /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $latest */
      $newRevision = $entityStorage
        ->loadRevision($latestRevisionId);
    }
  }
  if (!isset($newRevision)) {
    return;
  }

  // Get the correct language.
  $entityRevisionLanguage = $scheduledTransition
    ->getEntityRevisionLanguage();
  if ($entityRevisionLanguage && $newRevision instanceof TranslatableInterface && $newRevision
    ->hasTranslation($entityRevisionLanguage)) {
    $newRevision = $newRevision
      ->getTranslation($entityRevisionLanguage);
  }
  $this->logger
    ->debug('Latest revision was loaded for for scheduled transition #@id', [
    '@id' => $scheduledTransition
      ->id(),
  ]);
  $event
    ->setNewRevision($newRevision);
  $event
    ->stopPropagation();
}