Comments

ryan osītis’s picture

Component: Documentation » Code
Category: Support request » Feature request
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.4 KB

Workbench Moderation Notes doesn't seem to have tokens available to the Rules module by default. I've written this patch to add support for a Notes token.

murat_halici’s picture

I applied the patch, but I'm having trouble sending the token value in an email.

My Rule is setup as follows:
Event: After moderation transition
Element 1: Content's previous moderation state = Draft
Element 2: Content's current moderation state = Needs Review
Action: Send mail

The email is successfully sent, however the token value is empty. I know it's not an issue with the patch because when I goto node/[nid]/devel/token using the Devel module, I see the correct value of the token.

Denis Danielyan’s picture

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

I can verify #2's problem.
The Issue is that in
workbench_moderation_notes_form_workbench_moderation_moderate_form_alter_submit
The state change is called right at the beginning of the function to prepare the new state for the note to be added.
When the state change code is called, the e-mails are triggered.
At this point the note has not yet been written to the node or the db, so nothing comes up.

A admittedly quite hacky way of circumventing this is to store the note in a global variable right when the function is called:

  global $workbench_moderation_notes_note;
  $note = $form_state['values']['note'];  

Then in the function
workbench_moderation_notes_tokens
fetch the note when the token hook asks for it:

              global $note;
              $replacements[$original] = $note;

The second issue is, that when the state change comes from the submit button, the email (in my case using workbench_email) submit function is triggered before the notes_moderation submit trigger.

the Setup code in
workbench_moderation_notes_form_workbench_moderation_moderate_form_alter
removes the original submit trigger and adds the moderation_notes trigger to the end of the list.
This means that every other submit trigger (including email's) is called before.
A simple fix is to replace the original submit callback with the moderation_notes callback:

  foreach($form['#submit'] as $k => $callback) {
    if ($callback == 'workbench_moderation_moderate_form_submit') {
      $form['#submit'][$k] = 'workbench_moderation_notes_form_workbench_moderation_moderate_form_alter_submit';
    }
  }

Attached is the rerolled patch from #1 with my changes

Denis Danielyan’s picture

Denis Danielyan’s picture

sorry, wrong file attached ...

ajBNR’s picture

I've tried the patch from #5 multiple times and I keep getting this PHP error:

Notice: Array to string conversion in RulesTokenEvaluator->evaluate() (line 220 of /srv/bindings/a7f7a37529a540ef9dabc635868ae7f1/code/sites/all/modules/rules/modules/system.eval.inc).

In the email I send via Rules, the actual word 'Array' is written out. Any ideas?

cthshabel’s picture

Is there any update to this? I definitely see some value allowing the workbench note to be passed as a token. This way emails can be sent notifying users immediately of the moderation note within the email.

Thanks maintainers for an awesome module and everyone who contributes patches!

cthshabel’s picture

Okay. I also experience the same errors. I used them in the Workbench Emails module for emails. Here is the error:

Notice: Array to string conversion in token_replace() (line 102 of /var/www/html/veuit/includes/token.inc).

So, it seems something isn't working here.

Denis Danielyan’s picture

I just tried to repo your issues with a vanilla d7 (7.38) installation with the following modules:

Workbench (workbench) 7.x-1.2
Workbench Moderation (workbench_moderation) 7.x-1.4
Workbench Moderation Notes (workbench_moderation_notes) 7.x-1.x-dev
Workbench Email (workbench_email) 7.x-3.3

And everything worked well.
How do your setups differ from that?

cthshabel’s picture

I verified all versions match those you listed.

I still receive the error when the token is used in the workbench email messages.

I read the "Notice: Array to string conversion in token_replace()" error may be caused by another module setting multiple values for a token? But not sure what else would be setting the [node:workbench-moderation-note]?

I have been trying to troubleshoot this for a few days and just can't figure it out.

cthshabel’s picture

Denis, thanks for helping me look at this. Do you think it's due to the fact you removed these lines from the original patch here:

$state = '';
if (is_object($node) && property_exists($node, 'workbench_moderation') && isset($node->workbench_moderation['current'])){
$state = $node->workbench_moderation['current'];
}

Doesn't this mean the token is being set to the current revision note? Otherwise, won't the token contain all the notes from each moderation state change and it would be an array?

Just trying to think through this.

Thanks again

Denis Danielyan’s picture

I'm looking into it as we speak. I found one obvious issue on line that causes the note to be empty, but still looking for the issue with the error (yes, i can repo it now).

cthshabel’s picture

Yeah, I am playing with it now too.

I notice if I output the token like this:

drupal_set_message(var_export($replacements, 1));

before:

return $replacements;

then the output shows the token is empty also:

array ( '[node:workbench-moderation-notes]' => '', )

Denis Danielyan’s picture

So, found the issue.
I'm still not sure as to why but the token hook gets called twice.
The quite hacky solution is to clear the global value after it has been set once.

cthshabel’s picture

Works now!

I noticed you set the global variable in the previous patch, but never actually gave it a value? I take it that's why it was blank and returning an empty value? This is where you declare it:

global $workbench_moderation_notes_note;
$workbench_moderation_notes_note = $form_state['values']['note'];

That makes more sense now. Also, the token hook being called twice is what caused the token to become an array and the error to come up. How were you able to tell it was being called twice, just out of curiosity (hopefully, I could troubleshoot that better next time)?

Either way, thank you very much for your time and helping get that worked out.

Denis Danielyan’s picture

Yes, exactly, it seems i was in a hurry when creating the patch and tried to rename the global variable but didn't go all the way through.

How did I find out? I placed an "error_log()" function inside the token hook and was surprised why it got called twice. Of course I didn't pay any attention to it at first but after everything else didn't work out, this was my last straw...

hannes_bln’s picture

For me, tokens within e-mails got properly replaced for the first recipient only (because after the first replacement, the global var was "null").

The attached patch should work around that limitation.
The code is not becoming prettier though ...

gluebox’s picture

With the latest patch -17 I am only able to get "Array" from the token replacement in emails. To get beyond this I've rolled back to patch -14. In my rules I can get to my moderation_notes token just one, then the token data goes away (?). So my workaround is to setup a variable and fill it with my moderation_notes token, then I can use and re-use my variable token in different parts of my rules. Glad that there's a work around, thanks to everyone above for their help!