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.

Drupal 8 Workflow Notifications with Rules Part 1

For Drupal projects I’ve always used the comprehensive suite of tools provided by Workbench. But when I started a new project and saw that Workflow is hopefully making it’s way into core it felt like a good time to try it out. One part of the project was the addition of notifications when Workflow states were changed. With the Rules module also receiving lots of attention I decided combining the two modules was the best approach.

The following example was made using Drupal 8.4.4

Enable the Modules

First of all we have to enable the Workflows module and the Content Moderation module. Click on Extend form the admin menu or go to /admin/modules and find these two modules.

When enabling Content Moderation you will be warned that it is an experimental module. https://www.drupal.org/core/experimental

Edit the Workflow

If you go to /admin/config/workflow/workflows you will see we have one workflow called Editorial.

List of available Workflows

Click the Edit button to go to /admin/config/workflow/workflows/manage/editorial

States in the Editorial Workflow

We currently have three states but we want to add a new one for Review. Click the Add a new state link. On the Add state page all we need to enter is a label of Review and then click Save.

Adding a new Workflow state

Back on the Edit Editorial Workflow page drag the Review state to in between Draft and Published and then click the Save button at the bottom of the page.

Reordering the Workflow States

We now need to edit the transitions to allow the flow between draft, review and publish. This is how the transitions look by default. We need to enable a draft to be become a review and a review to become published. We want to cut out the draft to published transition.

Below are the default transitions, we’ll first add our new ones and then modify the existing ones.

List of the Editorial Workflow transitions

Click the Add a new transition link and you will be on a page with options for Label, From and To. Set the Label to Review, From to Draft and To as Review then click Save.

Adding a new Workflow transition

Back on the main page drag the new Review transition up so it is between Create New Draft and Publish and then click Save.

Reordering the Workflow transitions

We now need to update the existing transitions. Change the Create New Draft From value. It is currently Draft, Published and we need it to be Draft, Review.

Lastly for transitions change the Publish transitions’s From value to Review, Publish. This means a node has to go from Draft to Review before it can be published.

The completed transitions

Add The Workflow to Entities

The last part of the workflow setup is to enable it for any required content types or entities. At the bottom of the page there is the section This Workflow Applies To. In this example we will enable this workflow for Basic Pages.

Click the Select button beside Content types, select Basic Page from the overlay and then click Save. Now If you go to add a Basic Page you will see at the bottom there is a select where you can choose the State of you node.

Available Workflow states when saving a node

In the next part we will look at sending email notifications when the Workflow state changes using the Rules module.