You are here

public function ScheduledTransitionsUtility::generateRevisionLog in Scheduled Transitions 2.x

Generates a revision log for a ready to save revision.

Parameters

\Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface $scheduledTransition: The scheduled transition for the associated revision.

\Drupal\Core\Entity\RevisionLogInterface $newRevision: The entity a transition is created for.

Return value

string A revision log with replaced tokens.

Throws

\Drupal\scheduled_transitions\Exception\ScheduledTransitionMissingEntity Thrown if latest revision of a entity could not be determined.

Overrides ScheduledTransitionsUtilityInterface::generateRevisionLog

File

src/ScheduledTransitionsUtility.php, line 194

Class

ScheduledTransitionsUtility
Utilities for Scheduled Transitions module.

Namespace

Drupal\scheduled_transitions

Code

public function generateRevisionLog(ScheduledTransitionInterface $scheduledTransition, RevisionLogInterface $newRevision) : string {
  $entityStorage = $this->entityTypeManager
    ->getStorage($newRevision
    ->getEntityTypeId());
  $latestRevisionId = $entityStorage
    ->getLatestRevisionId($newRevision
    ->id());
  if ($latestRevisionId) {

    /** @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface $latest */
    $latest = $entityStorage
      ->loadRevision($latestRevisionId);
  }
  if (!isset($latest)) {
    throw new ScheduledTransitionMissingEntity(sprintf('Could not determine latest revision.'));
  }
  $options = $scheduledTransition
    ->getOptions();
  if (($options['revision_log_override'] ?? NULL) === TRUE) {
    $template = $options['revision_log'] ?? '';
  }
  else {
    $newIsLatest = $newRevision
      ->getRevisionId() === $latest
      ->getRevisionId();
    $settings = $this->configFactory
      ->get('scheduled_transitions.settings');
    $template = $newIsLatest ? $settings
      ->get('message_transition_latest') : $settings
      ->get('message_transition_historical');
  }
  $replacements = new ScheduledTransitionsTokenReplacements($scheduledTransition, $newRevision, $latest);
  $replacements
    ->setStringTranslation($this->stringTranslation);
  return $this
    ->tokenReplace($template, $replacements);
}