You are here

public function WorkflowState::getTransitions in Workflow 8

Returns the allowed transitions for the current state.

Parameters

\Drupal\Core\Entity\EntityInterface|null $entity: The entity at hand. May be NULL (E.g., on a Field settings page).

string $field_name:

\Drupal\Core\Session\AccountInterface|null $account:

bool|false $force:

Return value

\Drupal\workflow\Entity\WorkflowConfigTransition[] An array of id=>transition pairs with allowed transitions for State.

1 call to WorkflowState::getTransitions()
WorkflowState::getOptions in src/Entity/WorkflowState.php
Returns the allowed values for the current state.

File

src/Entity/WorkflowState.php, line 386

Class

WorkflowState
Workflow configuration entity to persistently store configuration.

Namespace

Drupal\workflow\Entity

Code

public function getTransitions(EntityInterface $entity = NULL, $field_name = '', AccountInterface $account = NULL, $force = FALSE) {
  $transitions = [];

  /** @var \Drupal\workflow\Entity\Workflow $workflow */
  if (!($workflow = $this
    ->getWorkflow())) {

    // No workflow, no options ;-)
    return $transitions;
  }

  // Load a User object, since we cannot add Roles to AccountInterface.
  if (!($user = workflow_current_user($account))) {

    // In some edge cases, no user is provided.
    return $transitions;
  }

  // @todo Keep below code aligned between WorkflowState, ~Transition, ~HistoryAccess

  /*
   * Get user's permissions.
   */
  $type_id = $this
    ->getWorkflowId();
  if ($user
    ->hasPermission("bypass {$type_id} workflow_transition access")) {

    // Superuser is special (might be cron).
    // And $force allows Rules to cause transition.
    $force = TRUE;
  }

  // Determine if user is owner of the entity. If so, add role.
  $is_owner = WorkflowManager::isOwner($user, $entity);
  if ($is_owner) {
    $user
      ->addRole(WORKFLOW_ROLE_AUTHOR_RID);
  }

  /*
   * Get the object and its permissions.
   */

  /** @var \Drupal\workflow\Entity\WorkflowConfigTransition[] $transitions */
  $transitions = $workflow
    ->getTransitionsByStateId($this
    ->id(), '');

  /*
   * Determine if user has Access.
   */

  // Use default module permissions.
  foreach ($transitions as $key => $transition) {
    if (!$transition
      ->isAllowed($user, $force)) {
      unset($transitions[$key]);
    }
  }

  // Let custom code add/remove/alter the available transitions,
  // using the new drupal_alter.
  // Modules may veto a choice by removing a transition from the list.
  // Lots of data can be fetched via the $transition object.
  $context = [
    'entity' => $entity,
    // ConfigEntities do not have entity attached.
    'field_name' => $field_name,
    // Or field.
    'user' => $user,
    // User may have the custom role AUTHOR.
    'workflow' => $workflow,
    'state' => $this,
    'force' => $force,
  ];
  \Drupal::moduleHandler()
    ->alter('workflow_permitted_state_transitions', $transitions, $context);
  return $transitions;
}