Drupal 8 Workflow Notifications with Rules Part 3

In previous posts I’ve covered enabling and editing a Workflow and then sending an email when a new content moderation state has been saved.

In this post we will complete the loop by sending emails when a content moderation state has been updated.

In this example an editor will publish the node and the rule will email the node creator to tell them it has been published.

Head to the Rules admin page /admin/config/workflow/rules and click Add reaction rule

This time we want to choose After updating content moderation state from the React on event select.

Adding a rules event for updating content moderation state

This time round we will need two conditions.

  1. Check the unchanged moderation state
  2. Check the new moderation state

Click the Add condition button and then choose Data comparison from the select.

For the data to compare value you can either use the data selection or enter

content_moderation_state_unchanged.moderation_state.value

Then for the data value enter review

This is the moderation state the node was in before it was saved.

Now click save.

Moderation state unchanged data comparison condition

Add another data comparison condition then this time enter the following

Data to compare value

content_moderation_state.moderation_state.value

Data value

published

New moderation state data comparison condition

Next we need to fetch the entity so we can use it’s values in our email.

Back on the edit page for this rule click Add action and choose Fetch entity by id from the select.

For Entity type enter node and for the Identifier value enter

content_moderation_state.content_entity_id.value

Then click save.

Now we can send an email to the node creator.
Click Add action and choose Send email.

In the Send to field use direct input and enter

{{entity_fetched.uid.entity.mail.value}}

This is a token that gets replaced with the users email address.

For subject we can just enter something appropriate such as Published Notification

For the message we will use the direct input mode an a token that provides the entity title

Your content {{entity_fetched.title.value}} has been published.

Email action

Save that and then save the rule.

As is often the case with Drupal it’s worth clearing your cache before testing.

Rules is still in it’s infancy for Drupal 8 but I feel it provides enough functionality and is well enough supported that it can be included in production site.

5 thoughts on “Drupal 8 Workflow Notifications with Rules Part 3”

  1. Hi Ian, thank you for your tutorial. One last rule is missing for my setup and I can`t get it. How can I inform the author via a reaction rule, if the last revision he made is deletet and the content stays on his published state?

    Maybe this isn`t possible with rules, but its worth a question to a pro 😉

  2. Hi Kay,

    Not sure off the top of my head. Hopefully I’ll have a bit of time later in the week to see if it can be done.

    Thanks

    Ian

  3. Hi Kay,

    I’ve been looking at the rules and I don’t think this is possible at the moment.

    There are two reactions that I looked at but neither were triggered when I deleted a revision

    After deleting content
    After deleting content moderation state

    It would probably be possible to write some custom code using hook_entity_revision_delete

    Hope that confirms your thoughts.

    Ian

  4. The rule executes but the email is never delivered and I get an undefined error in the logs. I have tired hard coding the email address and that does not work either… I am not doing something right any help would be appreciated.

    Issues with Undefined Property on rule execution

    Drupal 8.6.14
    Rules 8.x-3.0-alpha4

  5. I’ve just done a test on Drupal 8.7.0 and I could get the email address when the rule fired.

    A few things to try.

    Set Logging and errors to All messages, with backtrace information. This could help pinpoint where it is failing.

    In the Actions make sure you are fetching the entity before the action trying to send the email.

    As a debugging step add an action of Show a message on the site. For the message value add the following

    Email {{entity_fetched.uid.entity.mail.value}}

    This should output the email address in a status message.

    And always clear the cache when the rule is updated.

    Are you able to get other emails sent from the site?

Leave a Reply

Your email address will not be published. Required fields are marked *