ScheduledTransitionsTokenReplacements.php in Scheduled Transitions 2.x
File
src/ScheduledTransitionsTokenReplacements.php
View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions;
use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface;
class ScheduledTransitionsTokenReplacements {
use StringTranslationTrait;
protected $scheduledTransition;
protected $newRevision;
protected $latest;
protected $moderationInformation;
protected $cachedReplacements;
public function __construct(ScheduledTransitionInterface $scheduledTransition, EntityInterface $newRevision, EntityInterface $latest) {
$this->scheduledTransition = $scheduledTransition;
$this->newRevision = $newRevision;
$this->latest = $latest;
}
public function getReplacements() {
if (isset($this->cachedReplacements)) {
return $this->cachedReplacements;
}
$entityRevisionId = $this->newRevision
->getRevisionId();
if ($this->latest instanceof ContentEntityInterface) {
$workflow = $this
->moderationInformation()
->getWorkflowForEntity($this->latest);
$workflowPlugin = $workflow
->getTypePlugin();
$states = $workflowPlugin
->getStates();
}
$originalNewRevisionState = $states[$this->newRevision->moderation_state->value ?? ''] ?? NULL;
$originalLatestState = $states[$this->latest->moderation_state->value ?? ''] ?? NULL;
$newState = $states[$this->scheduledTransition
->getState()] ?? NULL;
return $this->cachedReplacements = [
'from-revision-id' => $entityRevisionId,
'from-state' => $originalNewRevisionState ? $originalNewRevisionState
->label() : $this
->t('- Unknown state -'),
'to-state' => $newState ? $newState
->label() : $this
->t('- Unknown state -'),
'latest-revision-id' => $this->latest
->getRevisionId(),
'latest-state' => $originalLatestState ? $originalLatestState
->label() : $this
->t('- Unknown state -'),
];
}
protected function moderationInformation() : ModerationInformationInterface {
return $this->moderationInformation ?? \Drupal::service('content_moderation.moderation_information');
}
public function setModerationInformation(ModerationInformationInterface $moderationInformation) : void {
$this->moderationInformation = $moderationInformation;
}
}