You are here

public function WorkflowState::getOptions in Workflow 8

Returns the allowed values for the current state.

Parameters

object $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 $force:

Return value

array An array of sid=>label pairs. If $this->id() is set, returns the allowed transitions from this state. If $this->id() is 0 or FALSE, then labels of ALL states of the State's Workflow are returned.

1 call to WorkflowState::getOptions()
WorkflowState::showWidget in src/Entity/WorkflowState.php
Determines if the Workflow Form must be shown.

File

src/Entity/WorkflowState.php, line 463

Class

WorkflowState
Workflow configuration entity to persistently store configuration.

Namespace

Drupal\workflow\Entity

Code

public function getOptions($entity, $field_name, AccountInterface $account = NULL, $force = FALSE) {
  $options = [];

  // Define an Entity-specific cache per page load.
  static $cache = [];
  $entity_id = $entity ? $entity
    ->id() : '';
  $entity_type_id = $entity ? $entity
    ->getEntityTypeId() : '';
  $current_sid = $this
    ->id();

  // Get options from page cache, using a non-empty index (just to be sure).
  $entity_index = !$entity ? 'x' : $entity_id;
  if (isset($cache[$entity_type_id][$entity_index][$force][$current_sid])) {
    $options = $cache[$entity_type_id][$entity_index][$force][$current_sid];
    return $options;
  }
  $workflow = $this
    ->getWorkflow();
  if (!$workflow) {

    // No workflow, no options ;-)
    $options = [];
  }
  elseif (!$current_sid) {

    // If no State ID is given, we return all states.
    // We cannot use getTransitions, since there are no ConfigTransitions
    // from State with ID 0, and we do not want to repeat States.
    foreach ($workflow
      ->getStates('CREATION') as $state) {

      // #3119998

      /** @var \Drupal\workflow\Entity\WorkflowState $state */
      $options[$state
        ->id()] = html_entity_decode($this
        ->t('@label', [
        '@label' => $state
          ->label(),
      ]));
    }
  }
  else {
    $transitions = $this
      ->getTransitions($entity, $field_name, $account, $force);
    foreach ($transitions as $transition) {

      // Get the label of the transition, and if empty of the target state.
      // Beware: the target state may not exist, since it can be invented
      // by custom code in the above drupal_alter() hook.
      if (!($label = $transition
        ->label())) {
        $to_state = $transition
          ->getToState();
        $label = $to_state ? $to_state
          ->label() : '';
      }
      $to_sid = $transition->to_sid;
      $options[$to_sid] = html_entity_decode($this
        ->t('@label', [
        '@label' => $label,
      ]));
    }

    // Save to entity-specific cache.
    $cache[$entity_type_id][$entity_index][$force][$current_sid] = $options;
  }
  return $options;
}