I believe it would be very useful to integrate functionality into the rules module.
This patch is not complete.
State change actions are not implemented yet.
This patch adds a few rules events, such as the when a revision state has changed or when live content is unpublished.

However, during my searching for rules, I noticed the following loosely related threads:
- #1132882: Register rules trigger
- #1079134: Add triggers when states are entered/exited
- #1108502: Allow modules to alter the list of possible next states

I am not sure how exactly the rules module and drupals core 'trigger' functionality work together.
It may make sense to implement the triggers, such as state change actions, using drupal cores trigger functionality.
At the same time, if triggers are being removed in drupal 8, then it would make more sense to add action support for rules module directly so that D7/D8 differences do not matter.

So, how should this be handled?
I have attached a patch containing my initial rules integration support.

Comments

stevector’s picture

I haven't looked at this patch yet. So I'm not sure how it compares to the patches started in the related tickets you mention.

Testing the machine name patch and reporting any problems will help push the rest forward since Rules/Actions/Triggers will work a lot better if moderation state and transition names don't change out from under them.

#1226688: Rules Integration Support

BenK’s picture

Subscribing

rv0’s picture

sub

Frederic wbase’s picture

sub

kukle’s picture

sub

Vandalf’s picture

subscribing

liliplanet’s picture

subscribe thx!

pontus_nilsson’s picture

I am not sure if the label "Content is Live" is a good way of describing the state. By that I mean I haven't seen the phrase being stated before. Is there a more general used way of saying this in Workbench?

anon’s picture

I think WB calls that "Published".

lslinnet’s picture

Status: Active » Needs work
+++ b/workbench_moderation.rules.incundefined
@@ -0,0 +1,98 @@
+
+  return workbench_moderation_node_is_current($node);
+}

This will not return the live version, it will return the current version.

lslinnet’s picture

+++ b/workbench_moderation.moduleundefined
@@ -1400,6 +1400,10 @@ function workbench_moderation_moderate($node, $state) {
+  if (module_exists('rules')){
+    rules_invoke_event('workbench_moderation_after_changing_state', $node, $new_revision);

This is called even when the state haven't changed, is this intended behavior?

thekevinday’s picture

#8, #9:
Workbench seems to use the term "Live" to represent content that is both published and the "active" revision. I guess if it is unpublished but active, then it is not live?

#10:
The function documentation for "workbench_moderation_node_is_current" has:

/**
 * Utility function to determine if this node is in the live state.
 *
 * @param $node
 *   The node being acted upon.
 *
 * @return
 *   Boolean TRUE if this is the current revision. FALSE if not.
 */

"Utility function to determine if this node is in the live state." sounds like what the function is providing.
Perhaps "Live Revision" is different than simply if it is in the live state?

#8, #9, #10:
workbench_moderation_node_is_current function contents:

<?php
  if (!is_object($node)){
    if (isset($node->workbench_moderation['published']->vid) && isset($node->workbench_moderation['current']->vid)) {
      if ($node->workbench_moderation['published']->vid == $node->workbench_moderation['current']->vid) {
        return TRUE;
      }
    }
  }

  return FALSE;
?>

So "Current Version = Live Version"?
We (or maybe just me) seem to be having a terminology conflict.

I am just going to use "Content is Live Workbench Revision", but I am open to better, shorter, and/or easier wording. I am also adding "Workbench" in front of a few other text outputs to the attached patch. Let me know if this was a bad idea or not.

#11:
It was probably intended. I guess I was assuming the "workbench_moderation_moderate" function was only called if the state was changed. Better to spend the extra if tests to ensure that "..after_changing_state.." rule is only fired if the state was changed. That attached patch includes this change.

hedley’s picture

Was having some issues but changing:

+
+  if (module_exists('rules')){
+    if ($new_revision['from_state'] !== $new_revision['state']){
+      rules_invoke_event('workbench_moderation_after_changing_state', $node, $new_revision);
+    }
+  }

To:

+
+  if (module_exists('rules')){
+    if ($new_revision->from_state !== $new_revision->state){
+      rules_invoke_event('workbench_moderation_after_changing_state', $node, $new_revision);
+    }
+  }

Solved them, will roll a patch after reviewing further.

lslinnet’s picture

Have a patch incoming fixing that and adding another condition.

will post it as soon as I figure out how to actually create the patch file :)

thekevinday’s picture

Yep, it looks like I misread the following:
$new_revision = (object) array(
An array is defined, but then its suddenly cast to an object.

Also, I noticed the following possible problems with the "Content is Live Workbench Revision" rule:
- When creating a brand new node, that node is treated as "live" even if there is no "published" version. That is to say, when a node only has a draft and no live content, the draft is treated as live. Is this the correct behavior? (I created a bug report here: http://drupal.org/node/1274314)
- It seems that when a node is saved, the Live status is updated before the "Content is Live Workbench Revision" condition can be tested. What this means is that if a node was not live but then was made live, then the "Content is Live Workbench Revision" condition will be met. If the content was live before the node save, then the "Content is Live Workbench Revision" condition will not be met.

lslinnet’s picture

StatusFileSize
new4.94 KB

Here's the patch, fixing the issue with objects vs. arrays.
And adds a "Contents current state" condition which allows to check for what state the node is after it's updated.

thekevinday’s picture

There is a function called workbench_moderation_state_labels():

<?php
  $labels = &drupal_static(__FUNCTION__);

  if (!isset($labels)) {
    $labels = array();
    foreach (workbench_moderation_states() as $machine_name => $state) {
      $labels[$machine_name] = $state->label;
    }
  }

  return $labels;
?>

It looks like we can replace your workbench_moderation_rules_parameter_options_list_states() function with that one. Do you agree?

hedley’s picture

This is great, thanks guys was just about to start having a go at doing the same thing!

westbywest’s picture

subscribing

thekevinday’s picture

Status: Needs work » Needs review
StatusFileSize
new6.82 KB

I implemented support for setting the moderation state via rules.

It seems I had to implement hooks_action_info() for the workbench_moderation_set_state_action() to get this to work properly.

The only problem I found was that the hooks_action_info() does not allow passing the node to the workbench_moderation_set_state_action_form() function. This means that there is no way to load the current state to help guide the proper transaction order.

The attached patch should have both hedleys and lslinnet changes applied.
I also made the change I talked about in #17.

Status: Needs review » Needs work

The last submitted patch, workbench_moderation-1226688-20.patch, failed testing.

lslinnet’s picture

+function workbench_moderation_set_state_rules_action($node, $moderation_state) {
+  if (is_object($node) && !empty($moderation_state)){
+    actions_do('workbench_moderation_set_state_action', $node, array('state' => $moderation_state));
+  }
+
+  return array('node' => $node);
+}

Why aren't you using workbench_moderation_moderate($node, $state); to change the state? just like it is done in the function workbench_moderation_moderate_callback()?
It seems a little off topic to implement the action_info hook, especially when there is another issue here already working on implementing it.

theunraveler’s picture

Since work is happening also on integrating Workbench Moderation with the Trigger module (#1079134: Add triggers when states are entered/exited), perhaps that and the Rules integration should use a common interface (like a hook). I submitted a patch to that issue's queue that adds a hook that fires after a node is transitioned from one state to another. This patch should probably use that hook as well.

thomjjames’s picture

subscribing

fabianx’s picture

Status: Needs work » Needs review
thekevinday’s picture

Status: Needs review » Needs work

I am talking about two different kinds of "actions" in the post. There are the drupal core "actions" and the rules "actions". To try to keep myself from mixing the two up, I will refer to the drupal core actions as "core actions" and the rules actions as "rules actions".

#23
I can see workbench_moderation_trigger_transition() being of use.
I will need to re-diff the patch with yours applied first such that this patch depends on yours.
Before I do that, I/we need to make sense of how core triggers and core actions differ.
- http://api.drupal.org/api/drupal/modules--trigger--trigger.api.php/funct...
- http://api.drupal.org/api/drupal/modules--system--system.api.php/functio...

From what I understand, triggers are the front-end interface to core actions. They themselves are not the core actions. What I implemented here are the core actions.

If triggers are another word for "events", then the rules module conditions that match a given event will need to listen to that trigger you provided.
In the case of the performing a moderation transition via the rules module, it will need to call a core action.

#22, #23
workbench_moderation_moderate_callback() was being called, but via the core actions support I added.
The reason why I added the actions support is that no matter how many times I cleared cache in my browser and website (or did "drush cache clear all") I could never get the action I added for rules to appear in the menu. I then thought that rules in D7 might utilize the "actions" interface from drupal core for its own "actions" interface. The second I implemented that support, the rules action appeared after a page refresh without even clearing my caches. If this is not necessary, then perhaps there is some obscure bug on my end.

thekevinday’s picture

Status: Needs work » Needs review
StatusFileSize
new6.68 KB

Here is a patch that requires the patch from: http://drupal.org/node/1079134#comment-4975912 to be applied first.

It utilizes the workbench_moderation_workbench_moderation_transition() function provided by the trigger patch. This will cause the "needs review" tests to fail because that patch has not been applied upstream.

This patch still creates and uses core actions. This makes sense given that if we are going to use rules actions, we should do it properly via the functions supplied by drupal core. Given that the mentioned functionality does not yet exist, we then apply it. Should I create a separate feature request for core actions and make this depend on that as well?

Status: Needs review » Needs work

The last submitted patch, workbench_moderation-1226688-27.patch, failed testing.

thekevinday’s picture

Status: Needs work » Needs review
StatusFileSize
new4.58 KB

I moved the "action" parts to the trigger patch here: http://drupal.org/node/1079134#comment-4993588

Apply that patch first, then apply this one.
This one simply adds the workbench_moderation.rules.inc file that enables rules integration.
Nothing else is present in this patch.

westbywest’s picture

I see several patches in this thread, with some having been superseded. What is currently the order to apply patches to workbench_moderation 7.x-1.0?

It looks like this order ?
1 http://drupal.org/files/issues/workbench_moderation-1226688-20.patch
2 http://drupal.org/files/issues/trigger_support_for_wb_moderation-1079134...
3 http://drupal.org/files/issues/workbench_moderation-1226688-29.patch

thekevinday’s picture

I moved some of the code from #20 (from this thread) to the patch at #33 (from http://drupal.org/node/1079134#comment4993588) because core actions and core triggers are two different parts of the same whole. The rules integration support is a separate whole that then depends on those two parts.

Thus we have this patch order:
1. http://drupal.org/files/issues/trigger_support_for_wb_moderation-1079134...
2. http://drupal.org/files/issues/workbench_moderation-1226688-29.patch

thekevinday’s picture

StatusFileSize
new6.55 KB

This replaces previous patch in #29.

It has some minor code cleanups.
It contains a couple new features:
1) Added condition: "Content is using workbench moderation" that checks if workbench moderation is enabled for the content type of a given node.
2) Added action: "Load current moderation state" that provides a "workbench moderation" variable exactly as defined by the workbench module. This was necessary because the existing variable loading functionality in rules works with fields. workbench_moderation does not use fields to store its data. This provided variable appears to be an object.

itangalo’s picture

Status: Needs review » Needs work

Thanks for working on this!

I get AJAX errors when trying to use the following Rules components:

* Content is live revision (condition)
* Contents current moderation state (condition)
* Set moderation state (action)

I did add a new workflow state ("Archive") before trying this, but I don't think that should have mattered.

stopshinal’s picture

Status: Needs work » Needs review

I'm unable to quite get GIT to work, but I would love to apply this patch.

I've created the file: workbench_moderation.rules.inc in the workbench moderation module folder, and i've included the patch contents (though stripped to only include important code).

How do I let drupal know it's there now so Rules can access it?

thekevinday’s picture

Status: Needs review » Needs work

Whoops. According to #33 there is a bug.
Lets leave it at needs work until me or somebody else can find and solve that problem.

As far as git usage is concerned, I don't wish to go off topic so you should look into that yourself.

There is a quick tutorial for workbench_moderation+git here:
- http://drupal.org/project/workbench_moderation/git-instructions

There is detailed documentation here:
- http://drupal.org/documentation/git

tevans’s picture

subscribing

bryter’s picture

Subscribing

Taxoman’s picture

Subscribing (last one? ;-))

guillaumev’s picture

I just tried the patch provided and did not get any error... Itangalo > which version of rules are you using ? (I'm using 7.x-2.0)

itangalo’s picture

#39: I'm using dev, but if the bug is gone now I expect that it will be gone in dev as well.

guillaumev’s picture

Status: Needs work » Needs review
StatusFileSize
new13.11 KB

Here is a new patch taking both patch 32 and patch 29.

Itangalo > Can you try with rules 7.x-2.0 ?

itangalo’s picture

Patch tested! The AJAX errors in #33 are no longer present. Yay!

Frederic wbase’s picture

Also tested and works fine! Commit to dev version?

rv0’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new13.11 KB

Patch from #41 had 2 whitespace errors.. removed them manually (line 33/34)

alberto56’s picture

StatusFileSize
new73.16 KB

Hi all,

thanks for the patch. For now the only condition I see in the Rules UI is the node's current state, I would love to see the previous state (see enclosed image), so we can trigger to action through Rules only when we're moving from one specific state to another.

That might be for a different feature request though.

If I understand the patch correctly, the only way to access the previous state and do something is by creating a custom module, right, and not through the UI, right?

Cheers,

Albert.

alberto56’s picture

Also, small typo: "content's current moderation state" and not "contents current moderation state". (notice the apostrophe).

Taxoman’s picture

#45: yes, "previous content state" is absolutely desireable to have as an option there. Important.

thekevinday’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new6.33 KB

Fixed the typo.
Added "content's previous moderation state".

Please review and set this back to Reviewed & tested by the Community once (and if) you have applied and tested this.

alberto56’s picture

Status: Needs review » Needs work

@thekevinday,

the patch at #48, contrary to the one in #44, does not contain any reference to the workbench_moderation.rules.inc file, perhaps you forgot to add it to the patch?

Thanks,

Albert.

thekevinday’s picture

StatusFileSize
new14.31 KB

Oops, I mis-created the patch.
I had to do a git add and forgot to!

Which also means I lost the changes I made...

Lets try this again now.

alberto56’s picture

Status: Needs work » Reviewed & tested by the community

Nice work, works great for me. I added the patch, and created a rule that shows the message "Be careful, you are going directly from Draft to Published!", only if the previous state is draft and the current state is published.

It worked great, thanks,

Albert.

Frederic wbase’s picture

Also tested the patch and it works fine!

jvandooren’s picture

Patch works great for me too...

rv0’s picture

Works for me too

zweistein_13’s picture

Hi,
unfortunately patch does not work for me. We did install the most recent patch via GIT and it applied successfully but no new events are available in Rules (using newest workbench dev. version).

We also tried to install the trigger_support_for_workbench_moderation - 39 first. But the patch does not apply successfully and it throws the error-message: "error while searching for: return $output".

Can you help us how to install the patch rules integration patch successfully?

Thanks in advance!

alberto56’s picture

@zweistein_13 use the latest dev version of the module, patch using -p1, manually check that you patch has actually applied, then clear all your caches. Please provide the URL and a screenshot of the page where you expect to be seeing something you're not.

andreymaximov’s picture

Works great!

ericclaeren’s picture

Hi, made a condition too for workbench moderation, unfortunately I missed this topic :)

I'm seeing that some of my code is different, but maybe it may be helpfull. I used a list so you could select multiple states as a condition.

Thanks, cheers!

*damn, can't add files after you save the reply :)

/*
 * Implements hook_rules_condition_info().
 */  
function workbench_moderation_rules_condition_info() {
  $defaults = array(
    'parameter' => array(
      'node' => array('type' => 'node', 'label' => t('Content')),
    ),
    'group' => t('Workbench'),
    'access callback' => 'rules_workbench_moderation_node_integration_access',
    'module' => 'workbench_moderation',
  );
  $items['workbench_moderation_state_is'] = $defaults + array(
    'label' => t('Workbench state of content'),
    'help' => t('Evaluates to TRUE if the given content is of one of the selected content types.'),
    'base' => 'rules_workbench_moderation_state_is',
  );
  $items['workbench_moderation_state_is']['parameter']['states'] = array(
    'type' => 'list<text>',
    'label' => t('Content new workbench moderation state is'),
    'options list' => 'rules_workbench_moderation_states_options_list',
    'description' => t('The Workbench state(s) to check for.'),
    'restriction' => 'input',
  );
  return $items;
}

/**
 * Node integration access callback.
 */
function rules_workbench_moderation_node_integration_access($type, $name) {
  if ($type == 'event' || $type == 'condition') {
    return entity_access('view', 'node');
  }
}

/**
 * Provides the content type of a node as asserted metadata.
 */
function rules_workbench_moderation_state_is_assertions($element) {
  return array('node' => array('bundle' => $element->settings['states']));
}

/**
 * Options list callback for workbench moderation states.
 */
function rules_workbench_moderation_states_options_list($element) {
  $labels = workbench_moderation_state_labels();
  $array = (!empty($labels)) ? $labels : array();
  return $array;
}

/*
 * Condition: check for new state revisions of the node.
 */
function rules_workbench_moderation_state_is($node, $states) {
  $node_new_state = (isset($node->workbench_moderation_state_new) && $node->workbench_moderation_state_new) ? $node->workbench_moderation_state_new : '';   
  return in_array($node_new_state, $states);
}
alberto56’s picture

One thought about this: the "previous" and "current" state conditions should accept more than one input. Consider the following scenario (as is the case on my site):

- content can be deemed "important"
- content can be deemed "politically sensitive"
- content can be deemed "politically senstive and important".

All three of the above contents must be approved by someone with the role "supervisor", and then set to "needs review" for grammatical corrections.

If I want all my content that transitions from either "important" or "politically senstive and important" to "needs review" to be set to "sticky at top of lists", this can be done with this patch, but only by creating two rules. It would be nice (if not too complex) to be able to do it with one rule: to be able to set the "previous" state to EITHER "important" OR "politically sensitive and important".

I will leave this issue at RTBC, because it's still possible to achieve this currently, and once it's in perhaps another issue might be opened.

dams_26’s picture

Sorry, I'm begginer here, should I download and apply all of the patches or just the last one for use these integration features ?

I tried with the rules_integration-1226688-50.patch on the last dev version of the module but I still can't find filters like :
- Content is live revision
- Content moderation current state
etc.

thanks

alberto56’s picture

@dams_26, it should work. You might want to try clearing your Drupal cache under "Performance".

Good luck,

Albert.

dams_26’s picture

@alberto56 And it work's, thanks!!!

Thank you for this awesome feature, you're doing a great job.

jackalope’s picture

After a bunch of confusion about which patches to apply, I was able to get rules integration working with the most recent dev version and the patch from #50 only, without any of the trigger patches. It's working so I'm assuming I did it right! Thanks for all the work on this great feature, hope it gets committed soon.

thekevinday’s picture

Sorry for the late response.

#59
Multiple conditions are supported, but it is done through the rules interface and not the select list.
This can still be done in a single rule.
You should be able to use the "Add or" and "Add and" conditions, then add multiple "Contents current moderation state" conditions.

The rule would look something like the following:
Events:
- "After updating existing content"

Conditions
- "OR"
-- "Contents current moderation state" (with value = 'important')
-- "Contents current moderation state" (with value = 'politically sensitive and important')
-- "Contents current moderation state" (with value = 'needs review')

Actions
- "Make content sticky"

The (sparse) documentation for these "Complex conditions" are found here: https://drupal.org/node/1300034

elvin - albania drupal developer’s picture

#50 - worked like a charm!!!

thekevinday’s picture

Status: Reviewed & tested by the community » Needs work

There is an oversight that I need to address.

I am using the rules trigger event to auto-publish content from "Needs accessibility validation" to "Published" if it passes accessibility validation.
The user is not allowed to transition from "Needs accessibility validation" to "Published" and the rule automates this process.
The current implementation needs an "overide permissions/transitions" option so that it can be forcefully changed even if the current user is not allowed to do so.

thekevinday’s picture

Status: Needs work » Needs review
StatusFileSize
new15.02 KB

Here is the new patch with the 'force_transition' option.
This replaces all other patches, so you need only apply this one.

If you already have rules defined from the previous patches, you will need to resave each rule so that the 'force_transition' field can be properly set.

This patch is against the latest dev, but does still apply to 1.1.

ericduran’s picture

So I'm curious if the maintainer has any opinion on this patch.

I'm currently running a very small modified version of this patch which probably makes sense to separate into a separate issue.

I think the addition of the hook_workbench_moderation_transition is more important than rules support. Also if that hook gets added the rules support can be done in a separate module is needed.

Essentially what I'm saying is +1 hook_workbench_moderation_transition().

As a side note I did test this patch with rules and it seems to work well but I wont mark it RTBC because I didn't really do a thorough rules test.

johnpitcairn’s picture

I agree on the importance of hook_workbench_moderation_transition(). I often find Rules is complete overkill and adds far too much UI cruft for the sort of thing I want to do, which is generally better accomplished in my own custom module hook implementation.

So +1 from me on a patch that just supplies a hook for other modules to implement, and +1 on moving Rules integration to a separate module.

johnpitcairn’s picture

StatusFileSize
new15.94 KB

There's a problem with invoking hook_workbench_moderation_transition() solely where it is now (in the patch at #67).

It can run before the state transition is actually saved to the DB. If the node already has a published revision, the node_save() operation will be deferred from workbench_moderation_moderate() to a shutdown function, workbench_moderation_store(). So the hook needs to be invoked from there, and also conditionally from workbench_moderation_moderate().

This patch does that, incorporating the entire patch from #67.

I have tested this with my own custom module which loads data from {node} in an implementation of hook_workbench_moderation_transition() - this now works correctly, acting on the state-change regardless of whether the node had a published revision.

I have not tested if this change affects Rules functionality in any way, though I expect that it was in fact broken before, for the same reason.

thekevinday’s picture

I have not yet seen any new issues with the new patch (#70).

FYI:
I want to add rules support for 'current' content as well as 'live' content.
This cannot be done until the issue #1410966: workbench_moderation_node_is_current() does not actually check against the passed argument. gets resolved.

This patch provided here is affected by the issue because it directly calls workbench_moderation_node_is_current() function for the is_live() functionality.
The inconsistency can happen with the patch in this thread, so if you see this issue, try the patch at [# 1410966] first.
I also have a patch that utilizes the new workbench_moderation_node_is_live() functionality to make the rules triggers more consistent but that patch will have to wait until the said issue gets resolved.

Also, as far as the comment in #68 is concerned, the rules integration is passive.
One does not need the rules integration module installed with this patch because it is a soft-dependency (exactly like with views module integrations).
The file only gets loaded if the rules module is installed and enabled, thus you get no clutter.

thekevinday’s picture

Status: Needs review » Needs work

I have found a problem with patch #70.

The new save behavior does not properly save the entire node, corrupted fields and causing rules to fire improperly.
Patch #69 worked under the rules module without issue.

What I suspect might be happening is the following:
- Moderation state changes can happen without node saves.
- In particular, rules events are allowed to fire before node save and they are not supposed to perform any node save themselves.
- When the state gets changed via a "Before saving node" trigger, the #70 patch performs an unwanted save before all of the other rules have been processed.

For example, I have a rule that changes the url path based on the workbench access group and the moderation state.
These all need to be processed in the "Before Saving Node" trigger or extra, unwanted, saves will happen.
When the appropriate conditions are met, the url path will be changed based on properties from the workbench access taxonomy group.
The taxonomy group is an auto-updated hidden field inside of the node such that everything happens transparently for end users.
With the patch in #70, the save seems to happen before the rules finish executing, causing an undefined url path to be used instead of the actual url path.

This happened to me when I use the moderation tab moderate state change and not through an actual node edit/save.
(The page is located at /node/[nid]/moderate).

So for me, the #67 patch works properly during saves, but #70 breaks the save behavior. :(

johnpitcairn’s picture

Sounds like we need two hooks, doesn't it?

hook_workbench_moderation_transition($node, $old_state, $new_state)
Called at the end of workbench_moderation_moderate(), exactly as for #67, unaffected by the published condition, before the live revision (if any) is saved. This hook will always fire.

hook_workbench_moderation_live_revision_save($live_revision, $transitioned_revision, $old_state, $new_state)
Called from the shutdown function, after the live revision is saved. This hook will only fire if there is already a live revision, ie it will not fire when a node is first created as draft, or as a draft moves through additional states before it is first published.

I'll test this and supply a new patch.

johnpitcairn’s picture

Status: Needs work » Needs review
StatusFileSize
new15.96 KB

Patch as per #73, reverts to behaviour from #67, adds a new hook when the live revision is saved.

Rules integration does not implement the new hook, but that might be a desirable addition?

johnpitcairn’s picture

Further testing suggests I'd need to implement both hooks anyway, in which case it's better to just register my own shutdown function from hook_workbench_moderation_transition() - this will run after the node_save() since it's registered later.

And some thought about the idea of invoking hooks from a shutdown function leaves me a little uneasy, perhaps that's not a good thing to be doing?

In which case patches #70 and #74 should be ignored, I'm good with #67.

thekevinday’s picture

I noticed that there was a problem with one of my rules not performing as expected.
I think it somehow got corrupted or messed up on my end, because deleting it and re-creating it solved the problem.
I fixed my problem, made sure I had no clobbered code, and retried #70 and I did not have the pre-save issue from #72 again.
Apparently, the node save does not interfere with the pre-save process as I was believing it to.

It very well may be that the broken behavior you mentioned in #70 may have been broken in just the right way for me to not notice a problem on my end.

I am looking at the differences between the #70 and #74 and what concerns me now is if both hook_workbench_moderation_transition() and hook_workbench_moderation_live_revision_save().

What if we were to provide a single function in the same way you did #70, but document that the states should be payed attention to?
That is, explain that it is a good idea to check for $node->workbench_moderation['published'] or $node->workbench_moderation['current'] in the api documentation.

johnpitcairn’s picture

I'm wary of adding the hook invocation in a shutdown function, though I couldn't find any guidelines about that anywhere?

Assuming doing so is OK, the documentation should state that the hook is invoked when the state transition is complete, and that there may have been a deferred node_save() in a shutdown function, depending on whether there is a published revision. If a module's hook implementation needs to know whether it has been invoked pre-save or post-save (from shutdown), it needs to check isset($node->workbench_moderation['published']), which is the condition used to determine whether the hook is invoked from the transition or from the shutdown function.

Personally I think two hooks would be clearer, and perhaps one would have 'shutdown' in its name?

But I'm still good with #67, and that's easily documented - if you have code that must run after the possible node_save(), check the 'published' condition and register your own shutdown function.

This might at least provoke some thought and help stop cut-&-paste developers doing inadvisable stuff from a shutdown function, one that they won't know they're running inside if they blindly implement the hook in #70.

designingsean’s picture

#67 is working well for my needs.

rrscott426’s picture

I'm a nubie trying to implement an email action to a set of publishers triggered by various page modifications. I applied the patch from #70 post, but I don't see where I can set/create the events for workbench rules. I really need to get this action in place soon, can someone provide some assistance?

stevector’s picture

I've tested #67 on an in-progress site and it seems to work well. I'd be comfortable with committing #67 and addressing the question of of a shutdown hook in a follow up issue.

I'm attaching the patch I'm running on the aforementioned site. It also includes a 'workbench_moderation_set_state_during_save' action that I'm using in conjunction with feeds importing. I'm varying the moderation state based on some arbitrary conditions on the imported node and this seemed most easily accomplished by directly setting the workbench_moderation_state_new property.

For the purpose of clarity I think it'd be better to commit #67 and then address workbench_moderation_set_state_during_save in a follow up issue.

Kevin, John, anyone else, objections?

johnpitcairn’s picture

No objections from me, I have #67 running fine on a live site.

I don't think a shutdown hook is needed, if the docs for this hook point out that a node_save() may occur later and to handle that the implementing module can register its own shutdown function.

thekevinday’s picture

That is fine with me.

stevector’s picture

Status: Needs review » Reviewed & tested by the community

Great, commit to come in a few minutes.

stevector’s picture

Status: Reviewed & tested by the community » Fixed
adamdicarlo’s picture

Awesome!

Coincidentally, the new hook added in this commit allowed me to write a patch to fix a Superfish problem: #1484064: Nodes published through Workbench Moderation don't show in Superfish menu until caches cleared.

I'm not 100% sure it's a superfish issue, though -- could anyone here take a look at my patch and weigh in on that issue, please?

thekevinday’s picture

I guess at this point we take up the issue as mentioned in #80 at this thread: #1079134: Add triggers when states are entered/exited?
Change the state to bug report and re-create the patch against -dev?

Status: Fixed » Closed (fixed)

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

azarzag’s picture

#67 worked well for me

alexkb’s picture

Just tried the patch from #80 against 1.1 and setup rules and an email notification, and it works perfectly. Nice work guys. Hope this gets committed to the repo.

stevector’s picture

The patch has been committed to 7.x-1.x

jrsinclair’s picture

This is fantastic. Thank you.

jrsinclair’s picture

If anyone is needing to unpublish using rules, I have a patch for that: #1593792: Add 'unpublish' action to available rules actions

jrsinclair’s picture

I found a small bug, where the node object does not yet have a workbench_moderation property on first save, but does have a 'workbench_moderation_state_current' property set. The attached patch handles that scenario.

Is this the right place to post, or should I open a new issue?

mxh’s picture

#93 I'd recommend to create a new issue, so that your patch can be tested, worked out and ported to next workbench moderation release. If this problem is coming from this patch, you may just link in the new issue to this one. Thanks for your work!

jrsinclair’s picture

vchen’s picture

I don't know if this is the right place to ask, so I apologize in advance if this is the wrong place.

However, can I request to add a new action based on a moderation state? I'd like to lock a node from editing to certain roles depending on its current moderation state.

mxh’s picture

Hi vchen, just create a new issue with category "feature request". I think what you need is not an action but a feature where you can set permission restrictions for states. If you are familiar with programming drupal modules, you may take a look at hook_permission() and hook_node_access(). If not, just open a new issue.

mxh’s picture

Issue summary: View changes

Replaced direct links to issues with automatically generated ones.

divined’s picture

Issue summary: View changes

RulesEvaluationException: Argument previous_state is missing. in RulesPlugin->setUpState() (line 717 of /sites/all/modules/contrib/rules/includes/rules.core.inc).

Content created by features call "after_moderation_transition" event and return this error.

vladimir_kriukov’s picture

Here's a fix for #101 comment. For some reason $node->workbench_moderation['my_revision'] becomes array, not an object.
Then $old_revision->state returns as 'null'.

jweowu’s picture

I'm not sure why the revision data can be in array form -- fixing that at the source seems like the proper fix -- but in the interim it seems like ALL of the revision data is expected to be in object form, so this is an alternative to #102 which does that.

silverham’s picture

#101 seems to be the result of feature_ver_export exporting an array and treating it as, common when used with webform_features module.
I issued a follow issue with a new patch: #3090038: Rules Error caused by $node->workbench_moderation being a bunch of arrays, not objects