You are here

public function WorkflowState::getOptions in Workflow 7.2

Same name and namespace in other branches
  1. 7 includes/Entity/WorkflowState.php \WorkflowState::getOptions()

Returns the allowed values for the current state.

Parameters

string $entity_type: The type of the entity at hand.

object $entity: The entity at hand. May be NULL (E.g., on a Field settings page).

$field_name:

$user:

bool $force:

Return value

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

D7.x-2.x: deprecated workflow_field_choices() --> WorkflowState->getOptions()

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

File

includes/Entity/WorkflowState.php, line 436
Contains workflow\includes\Entity\WorkflowState. Contains workflow\includes\Entity\WorkflowStateController.

Class

WorkflowState
Class WorkflowState

Code

public function getOptions($entity_type, $entity, $field_name, $user, $force = FALSE) {

  // Define an Entity-specific cache per page load.
  static $cache = array();
  $options = array();
  $entity_id = $entity ? entity_id($entity_type, $entity) : '';
  $current_sid = $this->sid;

  // 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][$entity_index][$force][$current_sid])) {
    $options = $cache[$entity_type][$entity_index][$force][$current_sid];
    return $options;
  }
  $workflow = $this
    ->getWorkflow();
  if (!$workflow) {

    // No workflow, no 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() as $state) {
      $options[$state
        ->value()] = $state
        ->label();

      // Translation is done as part of defaultLabel().
    }
  }
  else {

    /* @var $transition WorkflowTransition */
    $transitions = $this
      ->getTransitions($entity_type, $entity, $field_name, $user, $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())) {
        $target_state = $transition
          ->getNewState();
        $label = $target_state ? $target_state
          ->label() : '';
      }
      $new_sid = $transition->target_sid;
      $options[$new_sid] = $label;

      // Translation is done as part of defaultLabel().
    }

    // Include current state for same-state transitions, except when $sid = 0.
    // Caveat: this unnecessary since 7.x-2.3 (where stay-on-state transitions are saved, too.)
    // but only if the transitions have been saved at least one time.
    if ($current_sid && $current_sid != $workflow
      ->getCreationSid()) {
      if (!isset($options[$current_sid])) {
        $options[$current_sid] = $this
          ->label();

        // Translation is done as part of defaultLabel().
      }
    }

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