Skip to content

Instantly share code, notes, and snippets.

@cpliakas
Created December 2, 2014 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpliakas/47ada642c4e6f45890f8 to your computer and use it in GitHub Desktop.
Save cpliakas/47ada642c4e6f45890f8 to your computer and use it in GitHub Desktop.
Patch to Drupal Workflow module to alter roles
diff --git a/includes/Entity/WorkflowState.php b/includes/Entity/WorkflowState.php
index 3b01bc1..c064adb 100644
--- a/includes/Entity/WorkflowState.php
+++ b/includes/Entity/WorkflowState.php
@@ -363,6 +363,9 @@ class WorkflowState extends Entity {
$roles = array_merge(array(WORKFLOW_ROLE_AUTHOR_RID), $roles);
}
+ // Allow modules to add or remove roles based on their own business logic.
+ drupal_alter('workflow_user_roles', $roles, $user, $entity_type, $entity);
+
// Set up an array with states - they are already properly sorted.
// Unfortunately, the config_transitions are not sorted.
// Also, $transitions does not contain the 'stay on current state' transition.
diff --git a/includes/Entity/WorkflowTransition.php b/includes/Entity/WorkflowTransition.php
index 0d5fdd0..3c0a945 100644
--- a/includes/Entity/WorkflowTransition.php
+++ b/includes/Entity/WorkflowTransition.php
@@ -279,6 +279,7 @@ class WorkflowTransition extends Entity {
// Make sure this transition is allowed by workflow module Admin UI.
$roles = array_keys($user->roles);
$roles = array_merge(array(WORKFLOW_ROLE_AUTHOR_RID), $roles);
+ drupal_alter('workflow_user_roles', $roles, $user, $entity_type, $entity);
if (!$this->isAllowed($roles, $user, $force)) {
watchdog('workflow', 'User %user not allowed to go from state %old to %new', $args, WATCHDOG_NOTICE);
// If incorrect, quit.
diff --git a/workflow.api.php b/workflow.api.php
index b64195f..379f778 100644
--- a/workflow.api.php
+++ b/workflow.api.php
@@ -219,3 +219,30 @@ function workflowfield_form_alter(&$form, $form_state, $form_id) {
// Get the current state and act upon it.
// .. copy code from the hook above.
}
+
+/**
+ * Implements hook_workflow_roles_alter().
+ *
+ * Allows implementing modules to alter the roles that are allowed to execute
+ * state transitions.
+ *
+ * @param array &$roles
+ * @param string $permission
+ */
+function hook_workflow_roles_alter(&$roles, $permission) {
+ if ('participate in workflow' == $permission) {
+ $roles['-2'] = '(' . t('my custom role') . ')';
+ }
+}
+
+/**
+ * Implements hook_workflow_user_roles_alter().
+ *
+ * Allows implementing modules to assign or remove a user's roles.
+ *
+ * @param array &$roles
+ * @param stdClass $account
+ */
+function hook_workflow_user_roles_alter(&$roles, $account, $entity_type, $entity) {
+
+}
diff --git a/workflow.module b/workflow.module
index 4d3725c..1d4fbe4 100644
--- a/workflow.module
+++ b/workflow.module
@@ -807,6 +807,7 @@ function workflow_get_roles($permission = 'participate in workflow') {
foreach ($roles[$permission] as $rid => &$role_name) {
$name = check_plain(t($role_name));
}
+ drupal_alter('workflow_roles', $roles[$permission], $permission);
}
return $roles[$permission];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment