Drupal 8 Workflow Notifications with Rules Part 2

In my previous post I talked through setting up Content Moderation with a review state. In this post we will complete the process by sending email notifications via the Rules module when the moderation state is changed.

Install Rules

To enable the Rules module we first need to require it via Composer. Run the following from the command line in the root of your project
composer require drupal/rules

Once this has run you can return to the Extend page and enable the Rules module.

Create the First Rule

By the end we will have three rules for different states and transitions. The first rule will handle the creation of content the state is set straight to Review.

To create a rule navigate to Manage -> Configuration -> Rules (/admin/config/workflow/rules)

Once on that page click on the Add a reaction rule button. Fill in the details with the following.

Label: Notify reviewers of content created for review

React on event: After saving new content moderation state

Selecting React on event: After saving new content moderation state

We now need to add a condition so click the Add condition button.

From the select box choose Data Comparison from under Data and then click Continue

Selecting Data Comparison condition

When adding our condition it is useful to Switch to data selection for the Data selector input. This will help us drill down into the available data.

Using data selection to find the required selector

The Data selector we want is content_moderation_state.moderation_state.value

Leave the Operator as ==

Set the Data value to review. This is the machine name of our state that was created in the Editorial Workflow.

Completed data comparison condition

We can now save our condition.

Adding an Action

Before we create our action to send the email we need to make the node available to our rule so we can use it’s title and ID in the email body.

Click Add action and then choose Fetch entity by id under the Entity section.

Adding a fetch entity by id action

On the next screen set the Entity Type value to node and then for the Identifier we will again switch to data selection.

Using the autocomplete we set the Identifier value to content_moderation_state.content_entity_id.value

Completed fetch entity by id action

Sending the email

We can now add an action for sending the email. Click the Add action button and choose Send email from the System section of the dropdown.

Choosing the send email action

For the Send To we use an email address that will forward to all reviewers, e.g. reviewers@example.com

For the Subject we use Item Ready For Review.

For the body we want to provide some information for our reviewers so we set it to

Please review {{ entity_fetched.title.value }} at https://example.com/node/{{ entity_fetched.nid.value }}

The two parts in curly brackets are tokens from our fetched entity, node, that we performed earlier.

When the email sends the body would would look something like

Please review My New Page at https://example.com/node/12

In the next part we will create two more rules to handle updating the moderation state.

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

  1. Hi Ian, Nice tutorial! Do you think there is a way to send email notifications to selected users instead of forwarding to all reviewers?

  2. There would be, you could try using conditionals in your rule to identify certain criteria and then send an email based on that.

    I think the hardest part of this is the management side of it. If you only have a few editors and a few reviewers it could all be quite manual. If editor ID = 1 email reviewer 2, if editor ID = 3 email reviewer 4 etc.

    But if you have lots of users and reviewers it could get complicated quickly. Maybe have a field attached to users of Review Group. Then based on that field in your rule you could email specific reviewers.

    If user’s review group = Group A email these three people, if it’s Group B email a different two reviewers.

  3. Thank you for pointing me in the right direction! But I’m wondering how I can add conditions to the Rule that refer to properties of the node that’s been updated. I added the “fetch entity by id” action, and I can select “fetched entity” in the data selector for a condition, but then I get the message, “Unable to get variable entity_fetched, it is not defined.” Evidently that entity has not been fetched yet at the time the conditions are tested. Is there a way around this?

  4. It would be really great if Rules got updated for Drupal 9. Seeing as that doesn’t appear to be happening anytime soon, do you know another method for crafting custom workflow notifications?

  5. I’ve done a very similar thing using hook_node_insert() and hook_node_update()

    I then used a combination of the current moderation state and the previous revision state.

    You would do something like this.

    $moderation_state = $node->get('moderation_state')->getString();

    // Load the last revision.
    $last_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($last_revision_id);

    // Get the last revisions state.
    $last_revision_state = $last_revision->get('moderation_state')->getString();

    if ($moderation_state = 'needs_review' && $last_revision_state == 'in_progress') {
    // Send a notification to the reviewer

Leave a Reply

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