You are here

public function StateTransitionValidation::getValidTransitions in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/StateTransitionValidation.php \Drupal\workbench_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\workbench_moderation\Entity\ModerationStateTransition[] Returns an array of moderation state transitions.

File

src/StateTransitionValidation.php, line 122

Class

StateTransitionValidation
Validates whether a certain state transition is allowed.

Namespace

Drupal\workbench_moderation

Code

public function getValidTransitions(ContentEntityInterface $entity, AccountInterface $user) {
  $bundle = $this
    ->loadBundleEntity($entity
    ->getEntityType()
    ->getBundleEntityType(), $entity
    ->bundle());

  /** @var \Drupal\workbench_moderation\Entity\ModerationState $current_state */
  $current_state = $entity->moderation_state->entity;
  $current_state_id = $current_state ? $current_state
    ->id() : $bundle
    ->getThirdPartySetting('workbench_moderation', 'default_moderation_state');

  // Determine the states that are legal on this bundle.
  $legal_bundle_states = $bundle
    ->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []);

  // Legal transitions include those that are possible from the current state,
  // filtered by those whose target is legal on this bundle and that the
  // user has access to execute.
  $transitions = array_filter($this
    ->getTransitionsFrom($current_state_id), function (ModerationStateTransition $transition) use ($legal_bundle_states, $user) {
    return in_array($transition
      ->getToState(), $legal_bundle_states) && $user
      ->hasPermission('use ' . $transition
      ->id() . ' transition');
  });
  return $transitions;
}