You are here

public function StateTransitionValidation::getValidTransitionTargets in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/StateTransitionValidation.php \Drupal\workbench_moderation\StateTransitionValidation::getValidTransitionTargets()

Gets a list of states a user may transition an entity to.

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\ModerationState[] Returns an array of States to which the specified user may transition the entity.

File

src/StateTransitionValidation.php, line 90

Class

StateTransitionValidation
Validates whether a certain state transition is allowed.

Namespace

Drupal\workbench_moderation

Code

public function getValidTransitionTargets(ContentEntityInterface $entity, AccountInterface $user) {
  $bundle = $this
    ->loadBundleEntity($entity
    ->getEntityType()
    ->getBundleEntityType(), $entity
    ->bundle());
  $states_for_bundle = $bundle
    ->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []);

  /** @var \Drupal\workbench_moderation\Entity\ModerationState $state */
  $state = $entity->moderation_state->entity;
  $current_state_id = $state
    ->id();
  $all_transitions = $this
    ->getPossibleTransitions();
  $destinations = $all_transitions[$current_state_id];
  $destinations = array_intersect($states_for_bundle, $destinations);
  $permitted_destinations = array_filter($destinations, function ($state_name) use ($current_state_id, $user) {
    return $this
      ->userMayTransition($current_state_id, $state_name, $user);
  });
  return $this->entityTypeManager
    ->getStorage('moderation_state')
    ->loadMultiple($permitted_destinations);
}