Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Drupal Rules How-to

You're reading from  Drupal Rules How-to

Product type Book
Published in Nov 2012
Publisher Packt
ISBN-13 9781849519984
Pages 74 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Robert Varkonyi Robert Varkonyi
Profile icon Robert Varkonyi

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.

arrow left Previous Chapter
You have been reading a chapter from
Drupal Rules How-to
Published in: Nov 2012 Publisher: Packt ISBN-13: 9781849519984
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}