A tour of the Drupal Rules module for automating website actions

A tour of the Drupal Rules module for automating website actions

01 فبراير 2021

Imagine a smart website that automatically performs the right actions in response to particular events. Even complex scenarios are easily achievable. The cherry on top of the cake is that all this is possible without long lines of code — all you need is your Drupal admin dashboard. Interested? Welcome to get acquainted with the Drupal Rules module! See how one of the most popular modules in the Drupal development history can help you with website workflow automation.

The main principles of the Drupal Rules module’s work

The Drupal Rules module provides an interface for creating automated workflows on your website. You “teach” your site to perform streamlined and repeatable actions based on what is happening.

Event-condition-action (ECA)

The logic of the Drupal Rules module is built upon the event-condition-action (ECA) rules:

  1. There is an event that triggers the rule invocation.
  2. There is a certain condition that needs to be satisfied (optional).
  3. If (1) and (2) are true, the respective action is performed.

This can be presented as a “WHEN-IF-THEN” structure. A simple example would look as follows:

  1. Event (when): A blog post node is created.
  2. Condition (if): The node’s state is draft.
  3. Action (then): A notification is sent to the website’s moderator.

Optional conditions

As stated above, conditions are optional in the Drupal Rules formula. This means that you can, for example, ask your website to send a “thank you” message to all users who have submitted a form, no matter if they are anonymous or authenticated users and irrespective of any other conditions.

Unlimited conditions and actions

Drupal Rules allow you to require any number of conditions to be satisfied in order for the action to be triggered. Similarly, you can trigger an unlimited number of actions for the same event.

Ready and reusable components

In addition to the fact that the Drupal Rules module provides ready events, conditions, and actions to select from in the user interface, it also allows you to reuse whole Rules configuration sets. These are called Rules components and can be reused throughout your website so there is no need to create new ones from scratch for every scenario.

Some use cases for Drupal Rules

In addition to the simple examples above, the Drupal Rules module can be created for a wide range of situations. You can automate your website actions for these and many other use cases:

  • customized emails to users with important notifications
  • welcome messages, thank you messages, and so on
  • invitations to log in to view member-only content
  • all kinds of user instructions and explanations
  • page redirects
  • access denial
  • spam blocking and spam comment removal
  • notifications for the website’s admin
  • security alerts in case of suspicious actions
  • e-commerce discount calculations
  • notifications about products in stock
  • publishing or unpublishing scheduled content

The list could go on forever, and other modules that Rules integrate with add their own specific events, conditions, and actions. Depending on which extra modules are installed or core ones are enabled, the Drupal Rules UI shows the corresponding list of options.

For example:

  • with the core Media module enabled, you will see the options in Rules to configure what happens after updating, deleting, saving a media item, etc.
  • with the contributed Paragraph module installed, you will see the options in Rules to configure what happens after updating, deleting, saving a Paragraph, etc.

Some contributed modules come with submodules made specifically for the Drupal Rules integration. For example, the Scheduler Rules Integration is a submodule of the Scheduler module, which enables you to configure the publishing of your scheduled content based on some events and conditions.

The benefits of using the Drupal Rules module

  • All user interface and no coding. Rules put power into the hands of the end users and enable website owners to implement their business use cases, which makes it Drupal's most important design decision. So said Josef Dabernig, a major Drupal Rules contributor, in a discussion with Jeffrey McGuire from Acquia. “Rules eliminates the middleman” — this is a quote from the Rules Initiative interviews that Josef shared.
  • Saved time and improved efficiency. Just like any website automation, Drupal Rules can save plenty of time and improve the efficiency of the website owner’s team. There is no need to do standard and repetitive tasks anymore or monitor the routine events that your website can take care of.
  • Better user experience. Timely and useful messages, notifications, or instructions to the users make your website’s work transparent and clear, which is crucial for usability and customer loyalty.
  • Conversion growth. In addition to the fact that good user experience always transforms into conversions, there are cases when Drupal Rules can directly influence the purchasing decision. Informing the potential customer that the product appeared in stock can serve as a vivid example of how the right automated actions on a website can drive sales.
  • Increased security. Drupal Rules enable you to block unwanted or potentially dangerous actions. Even if there is no threat, being able to control access to the website’s pages is incredibly important.

Notes on Rules for Drupal 8 and 9

The Rules module has been ported to Drupal 8 and is ready for Drupal 9. However, due to the module’s complexity, the porting hasn’t been easy, and there was a call for developers to join in.
The Drupal 8 Rules module is still in the alpha state of development in 2021. It does not have all the features it used to have in Drupal 7, and many existing ones require significant work, according to Tim Rohaly, one of the module’s maintainers. He also introduced a new module called D8 Rules Essentials with an improved UI. The module is meant to provide missing pieces that create difficulties in using Rules for D8.

When we talk about the Drupal Rules module itself, one of the greatest architectural changes in Drupal 8 is that it switched to Twig-style tokens (for example, using {{ node.title }} instead of [node:title]. The tokens provided by the Token module are no longer used. More details about the tokenized data input are coming right now.

Simple Drupal Rules module tutorial

It’s time to see Drupal Rules in action — or, better to say, in event-condition-action! We will be showing it on the example of the 8.x module version.

Installing the Drupal Rules module

The Drupal Rules module will need at least one “companion” — you should install it together with the Typed Data module (AKA Typed Data API enhancements). In Drupal 7, this would be the Entity API module.

Creating Drupal rules

The Drupal Rules interface is found on the Workflow tab under Configuration. Here is where you will be able to find all rules that have been created on your website, both enabled and disabled. The “Add reaction rule” button takes you to the new rule creation interface.

  1. The first step of creating a rule is to select an event from the dropdown that you want your rule to be based on. You will also need to add a label for your new rule component and optionally add a description and tags, which could be helpful for finding the necessary components in the future.
    For example, we can create a rule to show a message to anonymous users when they view content. The event can be “Content is viewed.”
    With the event created, you arrive at the page where you can add conditions and actions.
  2. You can optionally add a condition. By clicking “Add condition” and you will be redirected to the page with a dropdown list of conditions to select from. Select one and continue. For example, the condition can be “User has roles.”
    Note: When configuring the condition, you will have a choice between the direct data input and data selection modes. The data selection mode will use tokens. This will enable you, for example, to refer to the current user contextually instead of putting in a specific name, refer to the site’s email instead of writing the actual address in the box, and so on. In the data selection mode, you will need to put the cursor into the “Data selector” box and go through the available options.
    For example, the condition can be to check the current user to see whether their role value is “Anonymous user.”
  3. Finally, you need to add the action that will be triggered if the condition is satisfied. Similarly to the condition creation, you can create an action by selecting the action type from the dropdown, clicking “Continue,” and configuring its values.
    For example, the action value can be the text of the message that will show on the site if the user role is anonymous — “Please log in to post comments”. Don’t forget to save the rule in the end.

Automate your website actions with Rules today!

The Rules module can become the best of all possible website automation tools for all who are using Drupal. Need a hand in configuring the actions and events? Send a ticket to our Drupal team and see what the property implemented website workflow automation can give you.

Believing in the power of smart websites,

Your Golems

Comments

An
مجهول