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
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
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.
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.
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.
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
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.
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.