You are here

class StateTransitionValidation in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/StateTransitionValidation.php \Drupal\content_moderation\StateTransitionValidation
  2. 10 core/modules/content_moderation/src/StateTransitionValidation.php \Drupal\content_moderation\StateTransitionValidation

Validates whether a certain state transition is allowed.

Hierarchy

Expanded class hierarchy of StateTransitionValidation

1 file declares its use of StateTransitionValidation
StateTransitionValidationTest.php in core/modules/content_moderation/tests/src/Unit/StateTransitionValidationTest.php
1 string reference to 'StateTransitionValidation'
content_moderation.services.yml in core/modules/content_moderation/content_moderation.services.yml
core/modules/content_moderation/content_moderation.services.yml
1 service uses StateTransitionValidation
content_moderation.state_transition_validation in core/modules/content_moderation/content_moderation.services.yml
\Drupal\content_moderation\StateTransitionValidation

File

core/modules/content_moderation/src/StateTransitionValidation.php, line 14

Namespace

Drupal\content_moderation
View source
class StateTransitionValidation implements StateTransitionValidationInterface {

  /**
   * The moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * Stores the possible state transitions.
   *
   * @var array
   */
  protected $possibleTransitions = [];

  /**
   * Constructs a new StateTransitionValidation.
   *
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
   *   The moderation information service.
   */
  public function __construct(ModerationInformationInterface $moderation_info) {
    $this->moderationInfo = $moderation_info;
  }

  /**
   * {@inheritdoc}
   */
  public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) {
    $workflow = $this->moderationInfo
      ->getWorkflowForEntity($entity);
    $current_state = $entity->moderation_state->value ? $workflow
      ->getTypePlugin()
      ->getState($entity->moderation_state->value) : $workflow
      ->getTypePlugin()
      ->getInitialState($entity);
    return array_filter($current_state
      ->getTransitions(), function (Transition $transition) use ($workflow, $user) {
      return $user
        ->hasPermission('use ' . $workflow
        ->id() . ' transition ' . $transition
        ->id());
    });
  }

  /**
   * {@inheritdoc}
   */
  public function isTransitionValid(WorkflowInterface $workflow, StateInterface $original_state, StateInterface $new_state, AccountInterface $user, ContentEntityInterface $entity = NULL) {
    if ($entity === NULL) {
      @trigger_error(sprintf('Omitting the $entity parameter from %s is deprecated and will be required in Drupal 9.0.0.', __METHOD__), E_USER_DEPRECATED);
    }
    $transition = $workflow
      ->getTypePlugin()
      ->getTransitionFromStateToState($original_state
      ->id(), $new_state
      ->id());
    return $user
      ->hasPermission('use ' . $workflow
      ->id() . ' transition ' . $transition
      ->id());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StateTransitionValidation::$moderationInfo protected property The moderation information service.
StateTransitionValidation::$possibleTransitions protected property Stores the possible state transitions.
StateTransitionValidation::getValidTransitions public function Gets a list of transitions that are legal for this user on this entity. Overrides StateTransitionValidationInterface::getValidTransitions
StateTransitionValidation::isTransitionValid public function Checks if a transition between two states if valid for the given user. Overrides StateTransitionValidationInterface::isTransitionValid
StateTransitionValidation::__construct public function Constructs a new StateTransitionValidation.