You are here

function workflow_field_choices in Workflow 7

Same name and namespace in other branches
  1. 5.2 workflow.module \workflow_field_choices()
  2. 5 workflow.module \workflow_field_choices()
  3. 6.2 workflow.module \workflow_field_choices()
  4. 6 workflow.module \workflow_field_choices()
  5. 7.2 workflow.deprecated.inc \workflow_field_choices()

Get the states current user can move to for a given node.

Parameters

object $node: The node to check.

boolean $force: A switch to enable access to all states (e.g. for Rules)

State $state: The predetermined state object (v7.x-1.3: new parameter for Workflow Field.)

Return value

Array of transitions.

Deprecated

workflow_field_choices() --> WorkflowState->getOptions()

1 call to workflow_field_choices()
_workflow_form_alter in ./workflow.node.inc
Used to Implement hook_form_alter(). Is now a subfunction of workflow_form_BASE_FORM_ID_alter(). This is more performant, since it is called only on form with correct BASE_FORM_ID.

File

./workflow.node.inc, line 375
Node specific functions, remnants of nodeapi.

Code

function workflow_field_choices($node, $force = FALSE, $state = NULL) {
  global $user;
  static $cache = array();

  // Node-specific cache per page load.
  $sid = isset($state->sid) ? $state->sid : 0;
  $nid = isset($node->nid) ? $node->nid : 0;
  $choices = array();
  if (!$node) {

    // If no node is given, no result (e.g., on a Field settings page)
    return $choices;
  }
  if ($nid && isset($cache[$nid][$force][$sid])) {

    // Do not cache new nodes (for security reasons).
    // @todo: add support for $entity_type.
    $choices = $cache[$nid][$force][$sid];
    return $choices;
  }
  if ($state) {

    // This is used in Field API.
    $workflow = Workflow::load($state->wid);
    $current_sid = $state->sid;
  }
  else {

    // This is used in Node API.
    $workflow = workflow_get_workflow_type_map_by_type($node->type);
    $current_sid = workflow_node_current_state($node);
  }
  if ($workflow) {
    $roles = array_keys($user->roles);

    // If user is node author or this is a new page, give the authorship role.
    if ($user->uid == $node->uid && $node->uid > 0 || arg(0) == 'node' && arg(1) == 'add') {
      $roles += array(
        'author' => 'author',
      );
    }
    if ($user->uid == 1 || $force) {

      // Superuser is special. And Force allows Rules to cause transition.
      $roles = 'ALL';
    }

    // Workflow_allowable_transitions() does not return the entire transition row. Would like it to, but doesn't.
    // Instead it returns just the allowable data as:
    // [tid] => 1 [state_id] => 1 [state_name] => (creation) [state_weight] => -50
    $transitions = workflow_allowable_transitions($current_sid, 'to', $roles);

    // Include current state if it is not the (creation) state.
    foreach ($transitions as $transition) {
      if ($transition->sysid != WORKFLOW_CREATION && !$force) {

        // Invoke a callback indicating that we are collecting state choices. Modules
        // may veto a choice by returning FALSE. In this case, the choice is
        // never presented to the user.
        // @todo: for better performance, call a hook only once: can we find a way to pass all transitions at once
        $result = module_invoke_all('workflow', 'transition permitted', $current_sid, $transition->state_id, $node, $field_name = '');

        // Did anybody veto this choice?
        if (!in_array(FALSE, $result)) {

          // If not vetoed, add to list.
          $choices[$transition->state_id] = check_plain(t($transition->state_name));
        }
      }
    }

    // Save to node-specific cache.
    $cache[(int) $nid][$force][$sid] = $choices;
  }
  return $choices;
}