Drupal - Run Custom Code When a Node's Workflow State Changes
Category:
Do you need to execute some custom code when a node's workflow state changes in Drupal? Yes? Ok.
In your custom module, implement hook_workflow provided by the Workflow module and try something like this:
/** * Implementation of hook_workflow() */ function my_module_workflow($op, $old_sid, $sid, $node) { // We will load all the workflows and iterate over each so it is easier for // developers to make changes to any workflow. $workflows = workflow_get_all(); if (isset($workflows)) { foreach ($workflows as $wid => $workflow) { switch ($op) { case "transition post": if ($workflow == "My Custom Workflow" || $wid == 123) { // Load up the workflow's states and determine // the old state and the current state. $worfklow_states = workflow_get_states($wid); $worfklow_old_state = $worfklow_states[$old_sid]; $worfklow_current_state = $worfklow_states[$sid]; drupal_set_message("$node->title had its workflow changed from $worfklow_old_state to $worfklow_current_state"); } break; } } } }
There are a few different $op values that are fed into your hook. So depending on what you are trying to do, you may want to use an $op value that is different from "transition post".