Components - Reusing Rules, Conditions, and Actions

Exclusive offer: get 50% off this eBook here
Drupal Rules How-to [Instant]

Drupal Rules How-to [Instant] — Save 50%

Discover the power of the Rules framework to turn your Drupal 7 installation into an action-based, interactive application with this book and ebook.

$12.99    $6.50
by Robert Varkonyi | January 2013 | Drupal Open Source

This article by Robert Varkonyi, author of Drupal Rules How-to, 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).

(For more resources related to this topic, see here.)

Getting ready

Enable the Rules and Rules UI modules on your site.

How to do it...

  1. Go to Confguration | Workfow | 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.

Summary

This article explained the benefits of using Rules components by creating a Condition that can be re-used in other rule configurations.

Resources for Article :


Further resources on this subject:


Drupal Rules How-to [Instant] Discover the power of the Rules framework to turn your Drupal 7 installation into an action-based, interactive application with this book and ebook.
Published: November 2012
eBook Price: $12.99
See more
Select your format and quantity:

About the Author :


Robert Varkonyi

Robert Varkonyi is a senior Drupal developer and author, originally from Hungary. He has worked with clients across the globe, such as NBC Universal and Ericsson. Robert is currently working with Tincan, a leading Drupal agency within the arts, cultural, and NfP sectors. He is also the author of Drupal Rules How-to.

Books From Packt


Drush User’s Guide
Drush User’s Guide

Drupal: Creating Blogs, Forums, Portals, and Community Websites
Drupal: Creating Blogs, Forums, Portals, and Community Websites

Drupal 7 Module Development
Drupal 7 Module Development

Drupal 7 Multilingual Sites
Drupal 7 Multilingual Sites

Drupal 7 Development by Example Beginner’s Guide
Drupal 7 Development by Example Beginner’s Guide

Drupal 7 Multi Sites Configuration
Drupal 7 Multi Sites Configuration

Drupal 7 Views Cookbook
Drupal 7 Views Cookbook

Drupal 7 Webform Cookbook
Drupal 7 Webform Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
y
C
A
m
U
3
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software