You are here

public function ScheduledTransitionsNewRevision::newRevision in Scheduled Transitions 8

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

Default new revision functionality.

Determines the new revision based on the revision ID on the scheduled transition entity.

Parameters

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

File

src/EventSubscriber/ScheduledTransitionsNewRevision.php, line 56

Class

ScheduledTransitionsNewRevision
Determines which revision should be transitioned.

Namespace

Drupal\scheduled_transitions\EventSubscriber

Code

public function newRevision(ScheduledTransitionsNewRevisionEvent $event) : void {
  $scheduledTransition = $event
    ->getScheduledTransition();
  $scheduledTransitionId = $scheduledTransition
    ->id();
  $targs = [
    '@id' => $scheduledTransitionId,
  ];
  $entity = $scheduledTransition
    ->getEntity();
  if (!$entity) {
    $this->logger
      ->info('Entity does not exist for scheduled transition #@id', $targs);
    return;
  }
  $entityRevisionId = $scheduledTransition
    ->getEntityRevisionId();
  if (!$entityRevisionId) {

    // Revision ID with zero value usually means revision is determined
    // dynamically with another subscriber.
    $this->logger
      ->debug('New revision will be determined dynamically for scheduled transition #@id', $targs);
    return;
  }

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

  /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $newRevision */
  $newRevision = $entityStorage
    ->loadRevision($entityRevisionId);
  if (!isset($newRevision)) {
    $this->logger
      ->info('Target revision does not exist for scheduled transition #@id', $targs);
    return;
  }
  $entityRevisionLanguage = $scheduledTransition
    ->getEntityRevisionLanguage();
  if ($entityRevisionLanguage && $newRevision instanceof TranslatableInterface && $newRevision
    ->hasTranslation($entityRevisionLanguage)) {
    $newRevision = $newRevision
      ->getTranslation($entityRevisionLanguage);
  }
  $event
    ->setNewRevision($newRevision);
}