What is the ECA module and what does it do?
The Events - Condition - Action (ECA) module allows you to perform an action based on a specific event that meets certain conditions.
If you have been using Drupal for a while, you are probably familiar with the Rules module. I used it a lot in Drupal 7, but to my knowledge, it never received a proper update to Drupal 8, 9, or 10. The ECA module fills this gap.
In this blog, I will use the example of sending an email when someone posts a comment on a Drupal website. This is useful to know if someone, for example, asks a question in a comment and you want to respond quickly.
If we plot this example on the ECA model, we get the following:
- The Event is the posting of the comment.
- The Condition is the requirement that the comment is posted by a user who does NOT have the administrator role. You can assume that comments posted by someone with an Administrator role do not need to be checked.
- The Action is sending an email to a specific email address.
Installation and configuration of the ECA module
First, we need to install the ECA module. After that, you need to install a so-called 'Modeller' module that allows you to link the Events, Conditions, and Actions together. You have various choices here. I started with the ECA Classic Modeller, but I couldn't get it to work properly. So I quickly switched to the BPMN.IO module, which makes the whole process visual using diagrams.
- After you have installed both modules, you can create a so-called 'model'.
- Navigate to Configuration > Workflow > ECA
- Click on 'Add new model'.
- On the right, you will see a box labeled 'Process'. Open the 'General' tab and give the entire process a name under 'Name'. In this case, I will enter 'Send email when a comment is posted'.
- On the left, you will see a menu with various shapes. The circle symbolizes the start of an Event. Therefore, drag a circle onto the white empty canvas.
- On the right, you will now see a box labeled 'Start event'. Click on the '+ select' button next to 'Template'. Here you can choose a template to start the process.
- In this example, it concerns adding a comment. A comment is considered a 'Content entity' within Drupal. Therefore, select 'Insert content entity'.
- In the box on the right, under the 'Custom properties' tab, you will see the option to specify a 'Type (any bundle)'. This allows you to determine what type of content entity will trigger the process. In this case, we choose 'Comment: -any-'.
- If you click on the 'General' tab, you can give the event a name. In this case, 'Comment is posted'.
- Next, drag the rounded rectangle (the 'Task') from the menu onto the canvas.
- Give the task a name in the box on the right under 'Name'. In this case, I will enter 'Send email'.
- Click on '+ select' next to 'Template' and search for 'Send email'.
- Under 'Custom properties' in the box on the right, enter the email address that should receive an email when a comment is posted in 'Recipient email address'.
- Under 'Subject', enter the subject of the email that will be sent. You can use so-called tokens here. For example, I use the following as a subject: '[comment:author] posted a comment on [comment:entity]'. When I receive an email, I can immediately see in the subject who posted the comment and the title of the node (blog or article) where the comment was made.
- In 'Message', enter what the content of the email should be. You can also use tokens here. I use the following: "[comment:body] Link: [comment:url]". Where the comment:body token shows the content of the comment, and with the comment:url token, I can navigate directly to the comment from the email.
- It may seem contradictory, but you can leave 'Replace tokens' set to 'no'.
- Next, select a line (sequence flow) on the left and draw it from the circle (event) to the square (task).
- Click on '+ select' under 'Sequence flow' on the right next to 'Template'.
- Search for and select 'Role of current user'.
- Select 'Administrator' under 'User role' and choose 'yes' for 'Negate condition'. This ensures that the action only takes effect if a comment is posted by a user who does NOT have the Administrator role.
- Now click the 'Save' button to save the model.
You can now test the model by posting a comment on your website and checking if the email arrives. If you do not receive an email, first check if your Drupal website is properly configured to send emails.
Do you have any questions or comments about this Drupal tutorial? Let me know by leaving a comment below.