Problem/Motivation

Is it possible to freely define the allowed transitions on a given state?
This is not intended as a guard condition. On the contrary, it is meant to open up new transitions, based on conditions that the core workflow module cannot verify since it restricts itself to roles-based transitions.

This patch opens up the possibility for 3rd party modules to create their own data-driven transitions based on other criteria. For example, one such module would alter allowed state transition per user. Custom modules can freely define the reasoning behind such rules.

API changes

A new hook 'hook_workflow_permitted_state_transitions_alter()' is created, allowing to add or remove allowed transitions from/to the proposed list. The list is used to generate the options in the Workflow widget. See file workflow.api.php for more info.

Original report by [username]

In my current usage of the workflow, I need to let a *specific user* execute a transition, based on a user reference field in the node instead of a user role. I also ran into other scenarios that are more indirect. I thus came up with a generic mechanism to allow modules to specific dynamic transitions, given a state, a user account, and a node. This patch is attached.

The way it works is by calling 2 new hooks at the relevant places. hook_workflow_transitions_alter alters the current allowable transitions to include new ones or stop existing ones. hook_workflow_transitions_info lists the modules that will intercept the transitions to display them on the admin transition grid.

A typical usage of these hooks would be thus:

<?php
// Define the sids we care about.
define('WF_STATE_1', 1);
define('WF_STATE_2', 2);
define('WF_STATE_3', 3);

function mymodule_workflow_transitions_info() {
  return array(
    WF_STATE_3 => array(
      WF_STATE_2 => t('Let the admin go back to state 2'),
      WF_STATE_1 => t('Let the referenced user go back to state 1'),
    ),
  );
}

function mymodule_workflow_transitions_alter(&$transitions, $sid, $account, $node) {
  if ($sid == WF_STATE_3) {
    if ($account->uid == 1) {
      $transitions[WF_STATE_2] = workflow_get_state_name(WF_STATE_2);
    }
    if ($node->field_user[0]['uid'] == $account->uid) {
      $transitions[WF_STATE_1] = workflow_get_state_name(WF_STATE_1);
    }
  }
}

?>

Thanks for your consideration. I'll be glad to enhance and polish this patch if needed.

CommentFileSizeAuthor
workflow-dynamic-transitions.patch4.53 KBinfojunkie
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maijs’s picture

Subscribing.

infojunkie’s picture

@maijs: why exportables?

maijs’s picture

Status: Postponed (maintainer needs more info) » Active

Sorry, nevermind.

k4ml’s picture

Mixing transitions info in db and code doesn't look right to me. Isn't this more like a guard condition on the transition, which should only been executed if the condition pass ?

infojunkie’s picture

@k4ml: no, this is not intended as a guard condition. On the contrary, it is meant to open up new transitions, based on conditions that the core workflow module cannot verify since it restricts itself to roles-based transitions.

This patch opens up the possibility for 3rd party modules to create their own data-driven transitions based on other criteria. For example, one such module would alter the workflow state transition grid to show CCK user reference fields for each state. Only users referenced in the selected field would be able to make the transition. This information would be stored in a separate database table, and the new module would open up this transition through the hooks introduced in the current patch.

kepford’s picture

Subscribing.

aquaphenix’s picture

Title: Dynamic transitions » Subscribing

Subscribing

infojunkie’s picture

Title: Subscribing » Dynamic transitions

Fixing title.

Bastlynn’s picture

Status: Active » Postponed (maintainer needs more info)

Interesting idea, can you roll me a patch against Drupal 7?

infojunkie’s picture

I'm afraid I'm not working with D7 at the moment.

johnv’s picture

I've added this to a meta issue:
#2144747: [META] Flexible transitions

colan’s picture

Version: 6.x-1.4 » 7.x-2.x-dev
Issue summary: View changes

This will have to go into D7 first.

colan’s picture

johnv’s picture

Title: Dynamic transitions » Allow dynamic/flexible/freely definable transitions per state by adding a newhook.
Issue summary: View changes

  • Commit a8607f5 on 7.x-2.x by johnv:
    Issue #0744272: Added new hook to allow freely definable transitions per...
johnv’s picture

Title: Allow dynamic/flexible/freely definable transitions per state by adding a newhook. » Allow dynamic/flexible/freely definable transitions per state
Version: 7.x-2.x-dev » 7.x-2.2
Status: Active » Fixed

Above commit adds a new hook, as described in the summary.

You can now create your own state machine with any custom logic you need.

infojunkie’s picture

Thanks for taking this to completion!

  • Commit 0cdfe70 on 7.x-2.x by johnv:
    Issue #0744272: Fixed sort order of states in Workflow Form was not...
johnv’s picture

Thanks, nice to have some feedback from the crowd.

Status: Fixed » Closed (fixed)

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