5

I have a rule that e-mails me if a user updates a value of a field on their profile (this value is the name that is displayed for that user on the site; I e-mail changes to the names to make sure no one changes their name to something insulting, etc.)

However, the rule fires twice for a reason I don't understand.

The rule has a condition compare data value in which it checks to see if the account-unchanged and account variables have different values for the same field. When I look at the rules debug log, I see that during Reacting on event 'After updating an existing user account', there is a second Reacting on event 'After updating an existing user account' within the first, and so the rule gets triggered twice. How can I prevent this from happening?

If it helps, here is the rule:

{ "rules_notify_if_user_changes_name" : {
    "LABEL" : "Notify if user changes name",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_update" ],
    "IF" : [
      { "NOT data_is" : {
          "data" : [ "account-unchanged:field-acc-displayname" ],
          "value" : [ "account:field-acc-displayname" ]
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[email protected]",
          "subject" : "[account-unchanged:field-acc-displayname] changed name to [account:field-acc-displayname] (UID: [account:uid])",
          "message" : "[account-unchanged:field-acc-displayname] changed name to [account:field-acc-displayname] (UID: [account:uid])",
          "language" : [ "" ]
        }
      }
    ]
  }
}

3 Answers 3

3

It sounds like some other module code is calling a user_save on hook_user_update triggering the rules event a second time.

1
3

It is a not ideal solution (meaning performance, rule still run twice) but IT WORKS!! I saw on drupal.org a patch for node module in core and did not like it. This solution need no code or patches. Only once my data is saved, I have a lot of modules enabled, so is very difficult to know which one is causing rules trigger twice. I added a hidden "Boolean Trick field" on my contentype (should work also on user bundle) and set it default value to true, you need conditional rules module to do it, and I did the following in rules, simple but effective:

enter image description here

4
  • Interesting trick, merci for sharing it! But, how about replacing that boolean field condition with another condition that checks if the last update date/time is at least X seconds before the current date/time. If that would work also, you'd not need to add such trick-field (though the only remaining question would be to find an appropriate value for X). Feb 3, 2017 at 17:54
  • 1
    Hi Pierre, this solution did worked for a sandbox proyect, then I found my problem and this solution did not make it to production. What you comment is posible, i'm thinking about a global variable instead of boolean field or the condition X time-value you refer to. let me give it a thought and if i come with a solution i will post it here. regards.
    – pinueve
    Feb 4, 2017 at 19:11
  • OK, look forward to your possible future post about it. If you do, make sure to leave a comment for me around here. Feb 4, 2017 at 19:14
  • 1
    Better idea: instead of a trick-field, global-var or time-stamp, flags it’s the best solution, you cuold play arrownd with flags and rules. This rule trick let’s you run the same rule several times having flags states the flow control, a ton of posibilities...
    – pinueve
    Mar 5, 2017 at 1:34
0

If you're able to execute PHP in your rules you can do this as a condition:

//lets see when we last changed this user $vnam = $account->uuid.":last_saved"; $retVal = TRUE; $t_changed = variable_get($vnam,0); $now = time(); variable_set($vnam,$now); //within 10 seconds, no second trigger if (($now - $t_changed) <= 10){ $retVal = FALSE; } return $retVal; // done saving changed timestamp

Your Answer

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

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