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)

Subscribe to comments on a node using Rules and Flag (Should know)


This recipe explains how to use Rules and Flag to send out e-mail notifications to users when someone comments on a node users are subscribed to.

Getting ready

Install and enable the Flag module.

How to do it...

  1. Create a new flag configuration at Structure | Flags.

  2. Enter a name and set the type to Nodes.

  3. Enter Subscribe as the label for the new Flag and set and Flaggable content to Article, as shown in the following screenshot, and save the Flag configuration:

  4. Create a new rule configuration at Configuration | Workflow | Rules, set the Event to Comment | After saving a new comment.

  5. Add an Action, Flag | Fetch users who have flagged a node, as shown in the following screenshot:

  6. Set the Flag to the new flag configuration we created.

  7. We want to act on the node the comment belongs to, so we'll use the comment's node in the Data selector field and save the Action, as shown in the following screenshot:

  8. Add a new loop in the Actions section and select users in the Data selector section, as shown in the following screenshot:

  9. Optionally, set the variable name to something that's more descriptive, as shown in the following screenshot, and save the loop.

  10. Add a new Action within the loop System | Send mail and configure the various fields using REPLACEMENT PATTERNS.

  11. Fill in the TO text area. Note, that we make use of a-user:mail token, which became available to Rules in the previous step, when defining the loop and setting the labels of the current list item.

  12. Enter the subject. Again, the a-user:name token is used, which will be replaced with the name of the user in the loop.

  13. Enter the message. Here we make use of other available tokens. This is shown in the following screenshot:

How it works...

In this recipe, we're creating a new flag configuration for article nodes and using that in our rule configuration to get the list of users that are subscribed to a node that's being commented on (using the Flag we created), and send them a notification e-mail. Flag provides a list data type (Fetch users who have flagged a node) that Rules can use to create a loop of all users who flagged a node, and act on each individual object.

There's more...

Flag provides various Events, Conditions, and Actions that we can use in our rule configurations.

Events

A node can be flagged or unflagged: This acts on Events that involve flagging or unflagging a node, user, or comment.

Conditions

The following are the Conditions provided by Flag:

Node/Comment/User is flagged: This checks if the entity is already flagged.

Node/Comment/User has flagging count: This checks the number of flags an entity has.

Actions

The Action for fetch users who have flagged a comment/node/user creates a list of users who have flagged an entity. The data will be provided to Rules as a list type, so it can execute a looped action on each individual object.

Flag a comment/node/user: This programmatically flags an entity.

Trim a flag: This sets the maximum number of flags an entity can have.

Unflag a comment/node/user: This programmatically unflags an entity.