I have created a rule (using the Rules module) using Rules Event "After updating existing comment" and some comment fields must be changed.

Loop node:comments is used but nothing happens when component is used to change a field (list_item is considered as Any entity (parameter) to use it in the component). node:comments doesn't work in that case. What can be done?

1 Answer 1


How to loop through comments using Rules

Have a look at this rule (in Rules export format, you should be able to import it in your own site if you have the Rules UI enabled):

{ "rules_change_a_comment_field" : {
    "LABEL" : "Change a comment field",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : { "comment_update" : [] },
    "DO" : [
      { "drupal_message" : { "message" : "An update to some comment has been discovered ... Some more details about this updated comment:\u003Cbr \/\u003E\r\n\u003Cbr \/\u003E\u003Cbr \/\u003E\r\nComment body (before) = [comment-unchanged:comment-body:value]\u003Cbr \/\u003E\r\nComment body (after) = [comment:comment-body:value]\u003Cbr \/\u003E\r\n\u003Cbr \/\u003E\u003Cbr \/\u003E\r\nComment author (before) = [comment-unchanged:author]\u003Cbr \/\u003E\r\nComment author (after) = [comment:author]\u003Cbr \/\u003E\r\n" } },
      { "LOOP" : {
          "USING" : { "list" : [ "comment-unchanged:node:comments" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "drupal_message" : { "message" : "Now processing comment with cid=[list-item:cid] and body = [list-item:comment-body:value] ..." } },
            { "entity_fetch" : {
                "USING" : { "type" : "comment", "id" : [ "list-item:cid" ] },
                "PROVIDE" : { "entity_fetched" : { "comment_fetched" : "Fetched comment" } }
            { "data_set" : {
                "data" : [ "comment-fetched:author" ],
                "value" : [ "site:current-user" ]
            { "entity_save" : { "data" : [ "comment-fetched" ], "immediate" : "1" } }

In this rule, for the sake of simplicity, I intentionally did not use a Rules Component (I just included my entire Rules logic in a single rule). Here is a summary of what the above rule does:

  • Rules Event: After updating an existing comment.
  • Rules Actions: Loop over each of the comments, and perform these actions for each of them:
    • Show a message on the site (just for demo purpose, to know what the rule is processing).
    • Fetch entity by id, to retrieve all data about the comment entity being processed in the loop.
    • Set a data value, to change the author of the comment being processed to the current user (this may seem a bit bizarre, I know, but consider it as just a demo sample, to illustrate you can update EACH comment processed in the loop).
    • Save entity, for the comment entity being processed in the loop. If you don't perform this action, then I'm pretty sure the updated value won't have been saved in the end (which is probably also what you experienced in your nothing happens).

Disclaimer: there may be more refined (optimized) solutions to get such rule to work, but using the above Rules actions (and meaningful variables ...) should help to understand what's happening if you're not experienced enough with Rules (yet).

Attention: be careful if you import the above rule (and leave it active) ... because this rule really works (and will change all comment authors to the last user who updated any comment).

Plan B

If what's above doesn't help, then something that will help you find the answer to your question, is to have a look at Rules debugging ... Within the Rules settings, experiment a bit with the debugging facilities that come with the rules module. At least have a look at the "Rules Evaluation Log" (to check that the Rules logic being executed, actually matches what you expect that logic to be).

Depending on what the result of that type of debugging is, you may also want to (temporary) enable the Devel module, so that you can (temporary) add a Rules Action like "Display a value" (in your case of your node:comments).

Refer to the (great!) video "Debugging rules configuration" for way more details about these 2 variations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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