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.
Create a new flag configuration at Structure | Flags.
Enter a name and set the type to Nodes.
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:Create a new rule configuration at Configuration | Workflow | Rules, set the Event to Comment | After saving a new comment.
Add an Action, Flag | Fetch users who have flagged a node, as shown in the following screenshot:
Set the Flag to the new flag configuration we created.
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:
Add a new loop in the Actions section and select users in the Data selector section, as shown in the following screenshot:
Optionally, set the variable name to something that's more descriptive, as shown in the following screenshot, and save the loop.
Add a new Action within the loop System | Send mail and configure the various fields using REPLACEMENT PATTERNS.
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.
Enter the subject. Again, the a-user:name token is used, which will be replaced with the name of the user in the loop.
Enter the message. Here we make use of other available tokens. This is shown in the following screenshot:
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.
Flag provides various Events, Conditions, and Actions that we can use in our rule configurations.
A node can be flagged or unflagged: This acts on Events that involve flagging or unflagging a node, user, or comment.
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.
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.