Book Image

Drupal Rules How-to

By : Robert Varkonyi
Book Image

Drupal Rules How-to

By: Robert Varkonyi

Overview of this book

Rules is what every Drupal site builder and developer has to use when creating event ñ action-based applications. The framework provides a highly flexible way to create sophisticated, condition-based functions any Drupal based system into an interactive application. Rules makes Drupal rule the CMS world."Drupal Rules How-to" is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you take advantage of the real power of the Rules framework, and understand how to use it on a site builder and developer levelThis book demonstrates the power and flexibility of the Rules framework. It discusses the main aspects of the module both from the site builder and developer perspective, from basic and advanced Rule configurations using Events, Conditions, Actions and Components to getting familiar with the Rules API. You will also learn how to use additional modules together with Rules to further extend the possibilities of your Drupal system, such as Rules Scheduler to schedule the execution of your Rule configurations and Views Bulk Operations to execute Rule configurations on a view result list. The book also demonstrates the main API features that enable you to create your own Events, Conditions and Actions, provide new data types to Rules and execute your configurations programmatically
Table of Contents (7 chapters)

Understanding the basics of Reaction Rules (Must know)

This section describes the basics of Reaction Rules, Events, Conditions, and Actions.

We'll create a simple rule that makes newly created articles sticky.

Getting ready

Enable the Rules and Rules UI modules on your site.

How to do it...

  1. Go to Configuration | Workflow | Rules.

  2. Click on Add new rule.

  3. Enter a name for this rule configuration, as shown in the following screenshot:

  4. Enter values for Tags if required (they can be useful for categorizing rule configurations).

  5. Set the Event to Node, after saving new content.

  6. Go to the Condition, Node | Content is of type and set the value to Article by selecting it in the select box.

  7. Add an Action, Data | Data selector and select the sticky field of the node.

  8. Hit Continue.

  9. Tick the Value checkbox.

  10. Click on Save.

How it works...

With the following steps, we're telling Rules to do the following: whenever a new content has been created and its content type is Article, set its sticky value to TRUE. This rule configuration will be executed every time a new article has been created.

There's more

Let's have a look at the way Events, Conditions, and Actions work.


A reaction rule always needs a specified event to happen on the site so it will execute. This can be done when a user logs in, when a node is created, or various other Events are provided by Rules (or other contributed/custom modules). Events may provide variables that can be used in the configuration. For example, if the event is Node | After saving new content, the created content object will be available in the rest of the rule configurations for Rules to work with.

A reaction rule can have multiple triggering Events. For example, we can execute the same Action when we delete a node or when we delete a comment.


We can use Conditions to check some data, that's available in our current configuration, because we usually want to execute an Action only if certain criteria are matching. For example, we might want to check a node's type (Content is of type), whether a node has a particular field (Entity has field) or a truth value (Data comparison). There are a number of Conditions provided by default, but it's also possible to create our own Conditions in our custom module.

Conditions can be grouped into AND or OR groups. These groups can be used to create complex Conditions and each group may have additional AND and OR groups.


Rules Actions are tasks that Rules may perform. There are a number of Actions that Rules provides by default, such as setting a value, publishing a node, or creating a new entity. Other than the core Actions, we can also create Actions in a custom module.