StateTransitionValidation.php

Same filename in other branches
  1. 9 core/modules/content_moderation/src/StateTransitionValidation.php
  2. 8.9.x core/modules/content_moderation/src/StateTransitionValidation.php
  3. 10 core/modules/content_moderation/src/StateTransitionValidation.php

Namespace

Drupal\content_moderation

File

core/modules/content_moderation/src/StateTransitionValidation.php

View source
<?php

namespace Drupal\content_moderation;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workflows\StateInterface;
use Drupal\workflows\Transition;
use Drupal\workflows\WorkflowInterface;

/**
 * Validates whether a certain state transition is allowed.
 */
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) {
        $transition = $workflow->getTypePlugin()
            ->getTransitionFromStateToState($original_state->id(), $new_state->id());
        return $user->hasPermission('use ' . $workflow->id() . ' transition ' . $transition->id());
    }

}

Classes

Title Deprecated Summary
StateTransitionValidation Validates whether a certain state transition is allowed.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.