You are here

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;

/**
 * Represents strings used as replacement variables in translation or logger.
 */
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;
  }

}

Classes

Namesort descending Description
ScheduledTransitionsTokenReplacements Represents strings used as replacement variables in translation or logger.