You are here

function workflow_node_previous_state in Workflow 7.2

Same name and namespace in other branches
  1. 8 workflow.module \workflow_node_previous_state()
  2. 7 workflow.module \workflow_node_previous_state()

Gets the previous state ID of a given entity.

3 calls to workflow_node_previous_state()
WorkflowTransitionForm::submitForm in includes/Form/WorkflowTransitionForm.php
workflow_node_current_state in ./workflow.module
Gets the current state ID of a given entity.
_workflow_tokens_get_transition in ./workflow.tokens.inc
Helper function to get the Transition.

File

./workflow.module, line 731
Support workflows made up of arbitrary states.

Code

function workflow_node_previous_state($entity, $entity_type, $field_name) {
  $sid = FALSE;
  $langcode = LANGUAGE_NONE;
  if (!$entity) {
    return $sid;

    // <-- exit !!!
  }

  // If $field_name is not known, yet, determine it.
  $field_name = workflow_get_field_name($entity, $entity_type, $field_name);
  if (is_null($field_name)) {

    // This entity has no workflow.
    return $sid;

    // <-- exit !!!
  }
  $previous_entity = NULL;
  if (isset($entity->old_vid) && $entity->vid - $entity->old_vid <= 1) {

    // Using the Revisioning module, get the old revision from DB,
    // if it is NOT the previous version.
    // The old revision from which to get our state, if it is not the revision
    // to which we want to switch.
    $previous_entity = entity_revision_load($entity_type, $entity->old_vid);
  }
  elseif (isset($entity->{$field_name}) && isset($entity->{$field_name}[$langcode][0]['workflow']['workflow_entity'])) {

    // Still using the Revisioning module, get the old revision from DB.
    $previous_entity = $entity->{$field_name}[$langcode][0]['workflow']['workflow_entity'];
  }
  elseif (isset($entity->original)) {
    $previous_entity = $entity->original;
  }
  if ($field_name === '') {

    // Workflow Node API: Get current/previous state for a Workflow Node.
    // Multi-language not supported.
    // N.B. Do not use a page cache. This gives problems with Rules.
    // Todo D7: support for Revisioning module.
    $sid = isset($entity->workflow) ? $entity->workflow : FALSE;
  }
  elseif ($field_name) {

    // Workflow Field API.
    if (isset($previous_entity)) {

      // A changed node.
      $wrapper = entity_metadata_wrapper($entity_type, $previous_entity);
      $sid = $wrapper->{$field_name}
        ->value();

      // Get language. Multi-language is not supported for Workflow Node.
      $langcode = _workflow_metadata_workflow_get_properties($previous_entity, array(), 'langcode', $entity_type, $field_name);
    }
    elseif (isset($entity->workflow_transitions[$field_name]->sid)) {

      // A new node. Upon save with Workflow Access enabled, the sid is needed
      // in workflow_access_node_access_records.
      $sid = $entity->workflow_transitions[$field_name]->sid;
    }
  }
  else {

    // Not possible. All options are covered.
  }
  if (!$sid) {
    if (!empty($entity->is_new)) {

      // A new Node. $is_new is not set when saving terms, etc.
      $sid = _workflow_get_workflow_creation_sid($entity_type, $entity, $field_name);
    }

    // Get Id. Is empty when creating a node.
    $entity_id = 0;
    if (!$sid) {
      $entity_id = entity_id($entity_type, $entity);
    }
    if (!$sid && $entity_id) {

      // Read the history with an explicit langcode.
      if ($last_transition = workflow_transition_load_single($entity_type, $entity_id, $field_name, $langcode)) {
        $sid = $last_transition->new_sid;
      }
    }
  }
  if (!$sid) {

    // No history found on an existing entity.
    $sid = _workflow_get_workflow_creation_sid($entity_type, $entity, $field_name);
  }
  return $sid;
}