You are here

public function ScheduledTransitionsRunner::runTransition in Scheduled Transitions 8

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

Executes a transition.

Ignores transition time as it is already checked by job runner.

Pass a transition that should be run. This method is responsible for loading latest and the new revision, then delegating saving new revision, and any intermediate revisions if applicable. The transition may also be deleted depending on settings.

Parameters

\Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface $scheduledTransition: A scheduled transition.

Throws

\Drupal\scheduled_transitions\Exception\ScheduledTransitionMissingEntity Thrown if any entity or entity revision is missing for a transition. Transition is never automatically deleted if exception is thrown.

Overrides ScheduledTransitionsRunnerInterface::runTransition

File

src/ScheduledTransitionsRunner.php, line 113

Class

ScheduledTransitionsRunner
Executes transitions.

Namespace

Drupal\scheduled_transitions

Code

public function runTransition(ScheduledTransitionInterface $scheduledTransition) : void {
  $scheduledTransitionId = $scheduledTransition
    ->id();
  $targs = [
    '@id' => $scheduledTransitionId,
  ];
  $entity = $scheduledTransition
    ->getEntity();
  if (!$entity) {
    $this->logger
      ->info('Entity does not exist for scheduled transition #@id', $targs);
    throw new ScheduledTransitionMissingEntity(sprintf('Entity does not exist for scheduled transition #%s', $scheduledTransitionId));
  }
  $event = new ScheduledTransitionsNewRevisionEvent($scheduledTransition);
  $this->eventDispatcher
    ->dispatch(ScheduledTransitionsEvents::NEW_REVISION, $event);
  $newRevision = $event
    ->getNewRevision();
  if (!$newRevision) {
    throw new ScheduledTransitionMissingEntity(sprintf('No revision could be determined to transition to for scheduled transition #%s', $scheduledTransitionId));
  }

  /** @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 */
    $latest = $entityStorage
      ->loadRevision($latestRevisionId);
  }
  if (!isset($latest)) {
    $this->logger
      ->info('Latest revision does not exist for scheduled transition #@id', $targs);
    throw new ScheduledTransitionMissingEntity(sprintf('Latest revision does not exist for scheduled transition #%s', $scheduledTransitionId));
  }
  $this
    ->transitionEntity($scheduledTransition, $newRevision, $latest);
  $this->logger
    ->info('Deleted scheduled transition #@id', $targs);
  $scheduledTransition
    ->delete();
}