I am upgrading a site from an older version of the workflow module and it has several custom pages that modify the workflow state of a node using workflow_transition(). This function is marked as deprecated in the latest version of the module, and the functions that are supposed to replace it make heavy assumptions about where the function is being used.

I'd like to know if there is an easy way to trigger a workflow transition without being on the node view or workflow tab pages.

Comments

hosef created an issue. See original summary.

johnv’s picture

The comments of the deprecated function should point to the new version.
In any case, you will find ample documentation in workflow.api.php file.

hosef’s picture

The deprecation note on workflow_transition() points to WorkflowDefaultWidget::submit(). WorkflowDefaultWidget::submit() assumes that we are submitting the workflow_transition_form and passing in the form and form_state arrays. I want to change the workflow state of a node without submitting the workflow_transition_form, which was possible in the old version of the module by calling workflow_transition($node, $new_sid);

Workflow.api.php describes how to respond to a transition that already happened and how to modify the workflow_transition_form, but it does not describe how to trigger a transition in the first place.

hosef’s picture

Status: Active » Fixed

After digging through the code some more, I found that I can use the WorkflowTransition entity class to force a state change on a node by doing something like:

$transition = new WorkflowTransition();
$transition->setValues('node', $node, $field_name, $node->workflow, $new_sid, $user->uid, REQUEST_TIME, $workflow_comment);
$transition->execute(TRUE);

If you are using the workflow_node module instead of the workflow field, just set $field_name to an empty string.

johnv’s picture

Title: How to progromatically change the workflow state of a node » How to programatically change the workflow state of a node

Indeed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

alan d.’s picture

When using fields, even easier: Replace field_kpi_workflow with the field name.


$node->field_kpi_workflow[LANGUAGE_NONE][0]['value'] = $needs_review;
node_save($node)

// Could not see how comments were done using this method, so blind DB update...
$nid_updated[$node->nid] = $node->nid;
db_update('workflow_node_history')
  ->fields(array(
    'comment' => 'Automated WorkFlow State Set Based On Year',
  ))
  ->condition('nid', $nid_updated)
  ->condition('entity_type', 'node')
  ->execute();