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.
This time round we will need two conditions.
- Check the unchanged moderation state
- 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.
Add another data comparison condition then this time enter the following
Data to compare value
content_moderation_state.moderation_state.value
Data value
published
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.
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.