You are here

function workflow_node_load in Workflow 7

Implements hook_node_load(). @TODO: Consider replacing with hook_entity_load().

File

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

Code

function workflow_node_load($nodes, $types) {

  // Get which types have workflows associated with them.
  $workflow_types = array_filter(workflow_get_workflow_type_map());
  foreach ($nodes as $node) {

    // If it's not a workflow type, quit immediately.
    if (!array_key_exists($node->type, $workflow_types)) {
      continue;
    }

    // ALERT: With the upgrade to Drupal 7, values stored on the node as _workflow_x
    // have been standardized to workflow_x, dropping the initial underscore.
    // Module maintainers integrating with workflow should keep that in mind.
    $last_history = workflow_get_recent_node_history($node->nid);
    if ($workflow = workflow_get_workflows_by_type($node->type)) {
      $node->workflow_wid = $workflow->wid;
      $node->workflow = $workflow
        ->getCreationSid();
    }

    // Nodes that existed before the workflow don't have any history.
    if ($last_history === FALSE) {
      $node->workflow_stamp = $node->created;
      continue;
    }
    else {
      $node->workflow = $last_history->sid;
      $node->workflow_stamp = $last_history->stamp;
      $node->workflow_state_name = $last_history->state_name;
    }

    // Add scheduling information.
    // Technically you could have more than one scheduled, but this will only add the soonest one.
    foreach (WorkflowScheduledTransition::load('node', $node->nid) as $scheduled_transition) {

      // Add the scheduled_transition as an object.
      $node->workflow_scheduled_transition = $scheduled_transition;

      // @todo: remove the separate '$node->workflow_scheduled' properties.
      $node->workflow_scheduled_sid = $scheduled_transition->sid;
      $node->workflow_scheduled_timestamp = $scheduled_transition->scheduled;
      $node->workflow_scheduled_comment = $scheduled_transition->comment;
      break;
    }
  }
}