1

I'm trying to send an email to users after their role has changed. So far, the rule is triggered every time the profile is saved (even without role change); I suspect that the data comparison is not handling the array correctly?!

Any help to improve the following rule or to achieve the same feature with a different condition will be very much appreciated.

{ "rules_role_changed" : {
    "LABEL" : "Role changed",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_update" : [] },
    "IF" : [
      { "NOT data_is" : { "data" : [ "account:roles" ], "value" : [ "account-unchanged:roles" ] } }
    ],
    "DO" : [
      { "mail" : {
          "to" : [ "account:mail" ],
          "subject" : "Role change for [account:name-raw]"
        }
      }
    ]
  }
}
1

To detect 'any' change to the users roles, you can use the following PHP via the condition "Use PHP code"

return (count(array_diff_key($account->roles, $account_unchanged->roles)) + count(array_diff_key($account_unchanged->roles, $account->roles))) <> 0;

The other PHP answers will not necessarily fire when a user has been 'removed' from a role.

-1

You always need to check the state of both unchanged and changed account, something like this:

{ "rules_role_changed" : {
"LABEL" : "Role changed",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "user_update" : [] },
"IF" : [
  { "NOT user_has_role" : {
      "account" : [ "account-unchanged" ],
      "roles" : { "value" : { "4" : "4" } }
    }
  },
  { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } }
],
"DO" : [
  { "mail" : {
      "to" : "lorem",
      "subject" : "ipsum",
      "message" : "dolor",
      "language" : [ "" ]
    }
  }
]
}
}

What this does is it checks if the user had the role before it was saved, and if the role has been added on the update - this means that user did not have the role before but does have it now. Here is a screenshot of the config: enter image description here

  • Thanks for your comment. However we're not talking about the same conditions: what I want to do is to detect any role change; what you discuss (as far as I understand) is to detect whether a given role (here "test role") has been added to the profile. Would you know how to do the former? – julou Apr 1 '15 at 6:40
-1

Thanks to this old revision of an answer to a similar question, I figured out that you can achieve the expected behaviour by comparing the array in plain php: https://drupal.stackexchange.com/revisions/52790/2

Select condition "Use PhP code" with e.g. the following snippet:

// keys only must be compared since values are different (!). testing using empty() produces an error, hence the comparison of count() to 0:
return count(array_diff_key($account->roles, $account_unchanged->roles)) <> 0;

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.