You are here

public function StateTransitionValidation::getValidTransitions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_moderation/src/StateTransitionValidation.php \Drupal\content_moderation\StateTransitionValidation::getValidTransitions()

Gets a list of transitions that are legal for this user on this entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to be transitioned.

\Drupal\Core\Session\AccountInterface $user: The account that wants to perform a transition.

Return value

\Drupal\workflows\Transition[] The list of transitions that are legal for this user on this entity.

Overrides StateTransitionValidationInterface::getValidTransitions

File

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

Class

StateTransitionValidation
Validates whether a certain state transition is allowed.

Namespace

Drupal\content_moderation

Code

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());
  });
}