Allow modifying of allowable transitions

Jaza - October 12, 2009 - 22:42
Project:Workflow
Version:6.x-1.1
Component:Code
Category:feature request
Priority:normal
Assigned:Jaza
Status:needs review
Description

I'm using Workflow with OG, and I want particular workflow transitions to be available only to users with OG admin privileges over particular nodes.

I was looking at the workflow_allowable_transitions() function, to see if there was any way to alter the results it returns. The only possibility seems to be via hook_db_rewrite_sql(); but considering the complex UNION SQL query that this function performs, even that is probably not possible (and anyway modifying the DB query directly is not ideal).

Attached patch adds a hook_workflow_allowable_transitions_alter(), via a simple one-line drupal_alter() call.

AttachmentSize
workflow_transitions_alter.patch331 bytes

#1

Jaza - October 12, 2009 - 22:56

FYI, here is my implementation of the new alter hook:

<?php
/**
* Implementation of hook_workflow_allowable_transitions_alter().
*/
function mymodule_workflow_allowable_transitions_alter(&$transitions, $sid, $dir, $roles) {
 
// List of 'to' transition states to check.
 
$sids = array(2, 3);

  if (
$dir == 'to' && in_array($sid, $sids)) {
   
// Get the node object for the current page (first param set to FALSE
    // means we don't have to be on a node view page, can also be on e.g. a
    // node editing or node workflow page).
   
$node = mymodule_get_curr_page_node(FALSE);

   
// If we're on a node page, and user is not a global admin, and user
    // is also not an OG admin for the current node, make certain transitions
    // unavailable.
   
if (!empty($node->nid) && !mymodule_check_role_access('administer nodes', $roles) && !og_is_group_admin($node)) {
     
// List of 'from' transitions to unset.
     
$unset_tids = array(4, 7, 8, 11);

      foreach (
$unset_tids as $tid) {
        unset(
$transitions[$tid]);
      }
    }
  }
}
?>

#2

Jaza - October 12, 2009 - 23:20

Oops, my hook implementation above doesn't work (teach me to test before posting snippets!). og_is_group_admin() needs to be passed the node of the group, not the node of the current page (which is assigned to the group). Once again, this is just FYI, for people who want to achieve the same OG to Workflow integration that I've set up:

<?php
/**
* Implementation of hook_workflow_allowable_transitions_alter().
*/
function mymodule_workflow_allowable_transitions_alter(&$transitions, $sid, $dir, $roles) {
 
// List of 'to' transition states to check.
 
$sids = array(2, 3);

  if (
$dir == 'to' && in_array($sid, $sids)) {
   
// Get the node object for the current page (first param set to FALSE
    // means we don't have to be on a node view page, can also be on e.g. a
    // node editing or node workflow page).
   
$node = mymodule_get_curr_page_node(FALSE);
    if (!empty(
$node->og_groups) && is_array($node->og_groups)) {
     
reset($node->og_groups);
     
$og_nid = key($node->og_groups);
     
$group_node = node_load($og_nid);

     
// If we're on a node page, and user is not a global admin, and user
      // is also not an OG admin for the current node, make certain transitions
      // unavailable.
     
if (!empty($group_node->nid) && !mymodule_check_role_access('administer nodes', $roles) && !og_is_group_admin($group_node)) {
       
// List of 'from' transitions to unset.
       
$unset_tids = array(4, 7, 8, 11);

        foreach (
$unset_tids as $tid) {
          unset(
$transitions[$tid]);
        }
      }
    }
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.