You are here

function _workflow_tokens_get_transition in Workflow 7.2

Helper function to get the Transition.

If the node is being created or updated (using the NUMERIC id), do not read from db, since the field widget has not been processed yet. @todo: solve this with module weight?

1 call to _workflow_tokens_get_transition()
workflow_tokens in ./workflow.tokens.inc
Implements hook_tokens().

File

./workflow.tokens.inc, line 350
Tokens hooks for Workflow module.

Code

function _workflow_tokens_get_transition($entity_type, $entity, $field_name) {
  global $user;
  $transitions = array();
  list($entity_id, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  $langcode = _workflow_metadata_workflow_get_properties($entity, array(), 'langcode', $entity_type, $field_name);
  if (!empty($entity_id) && !isset($entity->original)) {
    $transition = workflow_transition_load_single($entity_type, $entity_id, $field_name);
    return $transition;
  }

  // Get transition data from online-data.
  // Create dummy transitions, just to set $node->workflow_transitions[].
  foreach (_workflow_info_fields($entity, $entity_type, $entity_bundle) as $found_name => $field) {

    // If $field_name = NULL, any workflow_field/node is OK.
    if ($field_name != NULL && $found_name != $field_name) {
      continue;
    }
    $old_sid = FALSE;
    $new_sid = $found_name ? _workflow_get_sid_by_items($entity->{$found_name}[$langcode]) : $entity->workflow_sid;
    if (!isset($entity_id)) {

      // Creating an entity.
      // When creating a node, and only 1 valid sid is available, then the
      // widget is not shown. This generates a problem, since the new/old
      // sid isn't yet loaded in the Entity.
      if ($new_state = workflow_state_load_single($new_sid)) {
        $workflow = $new_state
          ->getWorkflow();
        $old_sid = $workflow
          ->getCreationSid();
      }
      else {

        // $field['settings']['wid'] can be numeric or named.
        $workflow = workflow_load_single($field['settings']['wid']);
        $old_sid = $workflow
          ->getCreationSid();
        $new_sid = $workflow
          ->getFirstSid($entity_type, $entity, $field_name, $user, FALSE);
      }
    }
    elseif (isset($entity->original)) {
      if ($field_name && !isset($entity->original->{$found_name}[$langcode])) {

        // When updating a node, that did not have a workflow attached before.
        // (Happens when you add workflows to existing Entity types.)
        $old_sid = workflow_node_previous_state($entity, $entity_type, $found_name);
      }
      elseif ($field_name) {

        // Updating an entity.
        $old_sid = _workflow_get_sid_by_items($entity->original->{$found_name}[$langcode]);
      }
      else {
        $old_sid = workflow_node_previous_state($entity, $entity_type, $found_name);
      }
    }
    $transition = new WorkflowTransition();
    $transition
      ->setValues($entity_type, $entity, $field_name, $old_sid, $new_sid, $user->uid, REQUEST_TIME, '');

    // Store the transition, so it can be easily fetched later on.
    // Store in an array, to prepare for multiple workflow_fields per entity.
    // This is a.o. used in hook_entity_update to trigger 'transition post'.
    $transitions[$field_name] = $transition;
  }
  $transition = $field_name && isset($transitions[$field_name]) ? $transitions[$field_name] : reset($transitions);
  return $transition;
}