4

I have a Drupal 7 website and I display comments on a content type.

What I want, is for every user to have a boolean field in their profile, to declare if they want to be notified when a new comment is posted at the content type that they posted.

I tried the Comment Notify module, but it shows its options right before you post your comment, and doesn't have a use with user profile/field.

I have already achieved a similar notification, for the user to be notified when the administrator replies to one of the user's comments.

I managed it using the Rules module, before a comment is posted, with these Rules Conditions:

  • Entity has field (comment:parent:author) and Data Comparison, to verify a relevant boolean at the user profile.
  • Data Comparison, for the (comment:author:roles) to be administrator.

Although, I can't seem to achieve the requested rule.

I know I have to do a search for every commenter at the same node, before a comment is posted, if they have the boolean enabled at their profile, but I can't create it with Rules.

To sum up, I have a content type (projects) and I have created about 50 of them. If some user X posts a comment on one of the projects and has the boolean enabled, then if anyone else comments on the same project, user X will be notified by email.

Any help?

  • 1
    What do you mean with "the content type that they posted"? Do you want to notify users when someone comments on one of their nodes? Or do you mean that users can comment on nodes and that they should be notified when someone else comments on the same node? Your title also contains "page", which is confusing. – Jeroen Sep 4 '17 at 15:13
  • I mean at the same node.I have a content type(projects) and I have created about 50 of them. If someone posts a comment on one of the projects and has the boolean enabled, then if anyone else comments on the same project, he will be notified by email. Sorry if it was confusing – Argiris A Sep 4 '17 at 17:22
4

Have a look at this rule (in Rules export format), which does what you're asking for:

{ "rules_notify_commentors_about_new_comment" : {
    "LABEL" : "Notify commentors about new comment",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional", "comment" ],
    "ON" : { "comment_insert" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "list\u003Cuser\u003E" },
          "PROVIDE" : { "variable_added" : { "users_that_commented" : "Users that commented" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "comment:node:comments" ] },
          "ITEM" : { "current_comment_item" : "Current comment item" },
          "DO" : [
            { "list_add" : {
                "list" : [ "users-that-commented" ],
                "item" : [ "current-comment-item:author" ],
                "unique" : "1"
              }
            }
          ]
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "users-that-commented" ] },
          "ITEM" : { "current_comment_author" : "Current comment author" },
          "DO" : [
            { "CONDITIONAL" : [
                {
                  "IF" : { "data_is" : {
                      "data" : [ "current-comment-author:field-accept-boolean" ],
                      "value" : "1"
                    }
                  },
                  "DO" : [
                    { "mail" : {
                        "to" : [ "current-comment-author:mail" ],
                        "subject" : "New comment to node with id [comment:node:nid]",
                        "message" : "There is a new comment added to the node with id [comment:node:nid]",
                        "language" : [ "" ]
                      }
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

The only additional module needed is the Conditional Rules module, while it does use a few of the more advanced techniques available in the Rules module.

If you're familiar with reading/digesting Rules in Rules export format, I'd hope the rule is self explaining. If not here is a summary of the above rule:

  • Rules Event: obvious ... after saving a new comment.
  • Rules Conditions: feel free to add whatever fits (eg limit to selected content types only, like "Projects" in this specific question).
  • Rules Actions:

    • Add a variable "users_that_commented" (= a list of users).
    • Loop over all existing comments, and for each comment author, add that author to the list of users (= users_that_commented). Note the "unique" that is turned on (we do not want duplicates in that list of users).
    • Loop over the list of users (= users_that_commented ... again, which we just created and populated). And for each user in that list, do your thing, in this case "send an eMail", provided the field field_accept_boolean is TRUE (which you can check if you have the Conditional Rules module enabled). The "To" value is crucial here, the body, the from, the title, etc is up to your own imagination. If you don't want to use the Conditional Rules module, you could also rework the Rules Action withn the last Rules Loop to performing Rules Components (in which you add the appropriate Rules Conditions), so that you don't need this extra module (for more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?").

If you want, just import the above rule in your own site (using the Rules UI), after you enabled the Conditional Rules module, and after replacing the field name of field_accept_boolean to fit the machine name of your own field.

That's really all it takes ... a Rules variable, Rules lists and loops, and Conditional Rules.

PS: There is one minor improvement of this rule I can think of (if perfect is not good enough) ... After anybody finds out what that may be, and after the question details are corrected about that also, I'd be happy to update my answer (= change 1 detail in the rule) to also implement that.

  • Way to go, I believe. No need for Flags, contrary to my no-time-to-open-a-testsite solution... – Jeroen Sep 4 '17 at 22:01
  • 1
    Merci for the kudos @Jeroen . To be honest that boolean field in the profile is the "dynamic" equivalent of the flag you suggested in your answer. And the major advantage of retrieving the boolean, is that at run time each user's profile is checked if the flag is still enabled. Nevertheless, your "Message"-addition is indeed a great suggestion (if combined with message_notify I believe). – Pierre.Vriens Sep 4 '17 at 22:10
2

You can achieve what you want with Flag and Rules. To notify users, you can use the Message module. Here is a possible outline to make things work:

  1. Create a node Flag Follow.
  2. Create a Message type New comment.
  3. Create a Rule that launches when a user commented on a node:
    • Check that the node's content type is Projects.
    • Fetch all users who flagged the node with Follow.
    • Loop through this list and create a New comment message for every user in the list.
    • (Bonus: depending on the text in your notification, you may want to set some of your message's fields. e.g. If you use Entity Reference, then you can refer to the user who commented and use his/her username in the message, for example.)
  4. Create a second Rule that launches when a user commented on a node:
    • Check that the node's content type is Projects.
    • Verify that the commenting user checked the checkbox for receiving notifications.
    • Let the commenting user flag the node with Follow.

Assuming that the above steps are clear enough, I will not add an export of the Rules in question. By the way, if you want to notify users with an email, then you can forget about the Message module and simply replace the action(s) in the loop of the first Rule by "Send an email".

  • Hey Jeroen, I think this answer is indeed a possible approach. But I also think you forgot to add some details about HOW a node gets (automatically) flagged by each user who commented on a node, no? Also, good to know that there is another "Message"-fan around here (please support my newest meta question about a tag for it ...). But only using the "Message" module, I wonder how "creating a message" is going to result in each comment author to actually be nottified. Maybe you mean "Message_notify" module (which depends on Message)? – Pierre.Vriens Sep 4 '17 at 20:26
  • 1
    @Pierre.Vriens: The second Rule will flag the node, such that the commenting user will get notifications afterwards. However, this flagging only works for new comments. It is still unclear for me whether the OP wants to find a solution that also works for already existing comments. As for your second remark: it is easy to create a View with unread Messages, which serves as an inbox. However, explaining how that could be done would lead us too far from the original question. – Jeroen Sep 4 '17 at 21:54
  • Well Jeroenn it's getting late around here (maybe come back on it tomorrow). But if "later on" a user decides to update his profile flag (Because user no longer wants to receive such notifies), then previous nodes are still flagged, so I seem to understand that the user still gets updates, even though the flag in the user profile is turned of. Understand my scenario? Or am I still missing something. About that "view for unread msgs": there are already multiple questions (I answered) about that, even one to combine it with "menu badges" ... slaap wel! – Pierre.Vriens Sep 4 '17 at 22:04
  • @Pierre.Vriens: You are right and I am aware of the fact that an extra Rule is required to unflag all nodes when a user updates his/her profile. However, I chose to not mention this until the OP reacts on my proposal/clarifies his question. Moreover, Messages are being removed automatically after a while, so I would personaly not be bothered if they still remained for a while in the View after 'unsubscribing'. – Jeroen Sep 4 '17 at 22:13
  • 1
    Using the flag module is indeed a valid alternative, though the "4."-part of this answer seems to require some furhter refinements. Nevertheless, +1 also for the Message-recommendation ... – Pierre.Vriens Sep 4 '17 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.