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)

Components – Reusing Rules, Conditions, and Actions (Must know)


This recipe explains the benefits of using Components by creating a Condition that can be re-used in other rule configurations.

In this scenario, we want to perform some action when a node is being commented on. But we only want to execute the action if the node was not created by the super admin (that is, user 1) and the node is either an article, or has an image field (field_image).

How to do it...

  1. Go to Configuration | Workflow | Rules | Components.

  2. Add a new component and set the plugin to Condition set (AND).

  3. Enter a name for the component and add a parameter Entity | Node.

  4. Add a Condition, Data comparison, set the value to the author of the node, set OPERATOR to equals, enter 1 in the Data value field and tick Negate.

  5. Add an OR group by clicking on Add or, as shown in the following screenshot:

  6. Add a Condition, Node | Content is of type and set it to Article.

  7. Add a Condition, Entity | Entity has field, set Entity to node, and select the field, field_image, as shown in the following screenshot:

  8. Organize the Conditions so that the last two Conditions are in the OR group we created before.

  9. Create a new rule configuration and set the Event to Comment | After saving a new comment.

  10. Add a new Condition and select the component that we created. An example is shown in the following screenshot:

  11. Select comment:node as the parameter.

  12. Add a new Action, System | Show a message on the site and configure the message.

How it works...

Components require parameters to be specified, that will be used as placeholders for the objects we want to execute a rule configuration on. Depending on what our goal is, we can select from the core Rules data types, entities, or lists.

In this example, we've added a Node parameter to the component, because we wanted to see who is the node's author, if it's an article or if it has an image field. Then in our Condition, we've provided the actual object on which we've evaluated the Condition. If you're familiar with programming, then you'll see that components are just like functions; they expect parameters and can be re-used in other scenarios.

There's more...

The main benefit of using Rules components is that we can re-use complex Conditions, Actions, and other rule configurations. That means that we don't have to configure the same settings over and over again. Instead we can create components and use them in our rule configurations.

Other benefits also include exportability: components can be exported individually, which is a very useful addition when using configuration management, such as Features.

Components can also be executed on the UI, which is very useful for debugging and can also save a lot of development time.

Other component types

Apart from Condition sets, there are a few other component types we can use. They are as follows:

  • Action set

    As the name suggests, this is a set of Actions, executed one after the other. It can be useful when we have a certain chain of Actions that we want to execute in various scenarios.

  • Rule

    We can also create a rule configuration as a component to be used in other rule configurations. Think about a scenario when you want to perform an action on a list of node references (which would require a looped Action) but only if those nodes were created before 2012. While it is not possible to create a Condition within an Action, we can create a Rule component so we can add a Condition and an Action within the component itself and then use it as the Action of the other rule configuration.

  • Rule set

    Rule sets are a set of Rules, executed one after the other. It can be useful when we want to execute a chain of Rules when an event occurs.

Parameters and provided variables

Condition sets require parameters which are input data for the component. These are the variables that need to be specified so that the Condition can evaluate to FALSE or TRUE.

Action sets, Rules, and Rule sets can provide variables. That means they can return data after the action is executed.