I need to theme node display differently based on the state of the node. How can I access the node state inside node.tpl.php
so I can show nodes with different state differently? What variables available there?

Comments

jvandyk’s picture

Status: Active » Closed (fixed)

Workflow 5.x-1.2's hook_nodeapi() implementation loads the current workflow state into $node->_workflow. This is an integer representing the state ID of the current workflow state for the node. That may be all you need.

If you need the actual state name, you can get it like this (but it will cost you a database query): $state_info = workflow_get_state($node->_workflow), which will return an array of information about the state. You could then see the state name in $state_info['state'].

gaellafond’s picture

NOTE:
In some case, the variable will be $node->workflow instead of $node->_workflow.
See: http://drupal.org/node/477780

If you just want the name, it seem to be faster to call workflow_get_state_name instead of workflow_get_state.

I got the following code to get the Workflow name:

if (isset($node->_workflow)) {
  $workflowId = $node->_workflow;
} elseif (isset($node->workflow)) {
  $workflowId = $node->workflow;
}

[...]

if (isset($workflowId)) {
  // I only display the workflow name in some cases.
  // So, I call workflow_get_state_name only when I need it.
  $workflowName = workflow_get_state_name($workflowId);
  [...]
}