You are here

class ScheduledTransitionsTokenReplacements in Scheduled Transitions 2.x

Represents strings used as replacement variables in translation or logger.

Hierarchy

Expanded class hierarchy of ScheduledTransitionsTokenReplacements

1 file declares its use of ScheduledTransitionsTokenReplacements
ScheduledTransitionsTokenTest.php in tests/src/Kernel/ScheduledTransitionsTokenTest.php

File

src/ScheduledTransitionsTokenReplacements.php, line 16

Namespace

Drupal\scheduled_transitions
View source
class ScheduledTransitionsTokenReplacements {
  use StringTranslationTrait;

  /**
   * A scheduled transition entity.
   *
   * @var \Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface
   */
  protected $scheduledTransition;

  /**
   * A new default revision.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface
   */
  protected $newRevision;

  /**
   * The latest current revision.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\RevisionableInterface
   */
  protected $latest;

  /**
   * Moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface|null
   */
  protected $moderationInformation;

  /**
   * An array of replacements.
   *
   * @var array|null
   */
  protected $cachedReplacements;

  /**
   * ScheduledTransitionsTokens constructor.
   *
   * @param \Drupal\scheduled_transitions\Entity\ScheduledTransitionInterface $scheduledTransition
   *   A scheduled transition entity.
   * @param \Drupal\Core\Entity\EntityInterface $newRevision
   *   A new default revision.
   * @param \Drupal\Core\Entity\EntityInterface $latest
   *   The latest current revision.
   */
  public function __construct(ScheduledTransitionInterface $scheduledTransition, EntityInterface $newRevision, EntityInterface $latest) {
    $this->scheduledTransition = $scheduledTransition;
    $this->newRevision = $newRevision;
    $this->latest = $latest;
  }

  /**
   * Get variables for translation or replacement.
   *
   * @return array
   *   An array of strings keyed by replacement key.
   */
  public function getReplacements() {
    if (isset($this->cachedReplacements)) {
      return $this->cachedReplacements;
    }
    $entityRevisionId = $this->newRevision
      ->getRevisionId();

    // getWorkflowForEntity only supports Content Entities, this can be removed
    // if Scheduled Transitions supports non CM workflows in the future.
    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 -'),
    ];
  }

  /**
   * Moderation information service.
   *
   * @return \Drupal\content_moderation\ModerationInformationInterface
   *   Moderation information service.
   */
  protected function moderationInformation() : ModerationInformationInterface {
    return $this->moderationInformation ?? \Drupal::service('content_moderation.moderation_information');
  }

  /**
   * Sets moderation information service.
   *
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderationInformation
   *   Moderation information service.
   */
  public function setModerationInformation(ModerationInformationInterface $moderationInformation) : void {
    $this->moderationInformation = $moderationInformation;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ScheduledTransitionsTokenReplacements::$cachedReplacements protected property An array of replacements.
ScheduledTransitionsTokenReplacements::$latest protected property The latest current revision.
ScheduledTransitionsTokenReplacements::$moderationInformation protected property Moderation information service.
ScheduledTransitionsTokenReplacements::$newRevision protected property A new default revision.
ScheduledTransitionsTokenReplacements::$scheduledTransition protected property A scheduled transition entity.
ScheduledTransitionsTokenReplacements::getReplacements public function Get variables for translation or replacement.
ScheduledTransitionsTokenReplacements::moderationInformation protected function Moderation information service.
ScheduledTransitionsTokenReplacements::setModerationInformation public function Sets moderation information service.
ScheduledTransitionsTokenReplacements::__construct public function ScheduledTransitionsTokens constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.