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)

Sending notifications if someone comments on a node created by another user (Must know)


If we want to send e-mails to individual users, it's better to use the Send mail Action in our rule configuration. A good use case could be when we want to notify a node's author when someone leaves a comment on a node created by them. We could then use the Event Comment | After saving a new comment, then add two Actions.

How to do it...

The following steps will help you send notifications if a user comments on a node created by another user:

  1. In the first Action, we need to load the author of the node that's being commented on. For that we can use Data | Add a variable Action and set the value of TYPE to User:

  2. We then set the value to the node's author.

  3. Optionally, we can set the label of the variable, so we can easily identify it in the next step:

  4. We add another Action, System | Send mail and use REPLACEMENT PATTERNS to make use of the variable we added in the previous step.

  5. We can then use node-author:mail in the To field which is the e-mail address of the original node author.

  6. If we don't want the author to be notified of their own comments, we can add a Condition, Data | Data comparison and set the DATA TO COMPARE value to the user ID of the node author, as shown in the following screenshot:

  7. Next, we hit Continue and set the data value to the user ID of the comment author, as shown in the following screenshot:

  8. This configuration is of course incorrect at this stage, as Rules would only send an e-mail if the Node author and the Comment author are the same, which is the opposite of what we're looking for. A really good feature in Rules is Negate. This setting basically sets TRUE values to FALSE and vice versa, which is just what we currently need. The Negate feature is shown in the following screenshot:

    This way the rule will only fire if the author of Node is not the same as the author of Comment.

There's more...

The following section will throw light on how to send notifications only to users wanting to receive them.

Sending notifications only if the user wants to receive them

For usability or to avoid our site users from feeling spammed, ideally we would want to add a new Boolean checkbox field to the user object that is used as a switch to indicate whether the user wants to receive these notifications from our site or not. Then in our rule configuration, we can use the Condition, Data | Data value is empty and set the Data selector value to that field. Assuming that the name of the field is field-notifications, it would look like the following screenshot:

This configuration is of course incorrect at this stage, as Rules would only send an e-mail if the field is empty (the user does not want notifications), which is the opposite of what we're looking for. So we need to use Negate .

This way the rule will only fire if that particular user has checked the Notifications field on his/her user account page.