1

I need to send an email when an admin changes a users role to role x. The email should not be sent if the users role is already role x and 'save' is pressed on the user page.

I have set up a rule:

Event: After updating an existing user account

Actions: Send mail

This works fine. Then I add a condition of type 'data comparison' account(updated user):roles with the operator 'equals' and the data value 'role x'. But for some reason the email now never sends when I change the user role to 'role x' and resave. I'm confused about why this is not working.

Then once this is fixed I assume I'll need to negate the condition that the account(unchanged user):roles equals 'role x'

2 Answers 2

2

Key according to me will be

Using condition as User has Roles choose "x" as user role and negate it.

An example export goes here.

{ "rules_update_user_role" : {
    "LABEL" : "Update User Role",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_update" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "account-unchanged" ],
          "roles" : { "value" : { "4" : "4" } }
        }
      }
    ],
    "DO" : [ { "drupal_message" : { "message" : "Changed User!" } } ]
  }
}
5

Arpitr got me on to the correct solution for this. Here is the code that you can import:

{ "rules_update_user_role" : {
"LABEL" : "Update User Role",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"ON" : { "user_update" : [] },
"IF" : [
  { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "7" : "7" } } } },
  { "AND" : [
      { "NOT user_has_role" : {
          "account" : [ "account-unchanged" ],
          "roles" : { "value" : { "7" : "7" } }
        }
      }
    ]
  }
],
"DO" : [ { "drupal_message" : { "message" : "Changed User!" } } ]

} }

I like the way arpitr used a drupal message for testing if the rule had worked - i didn't realise you could do this.

It looks like the mistake I might have been making was to use the condition of type 'data comparison' and then try to drill into it to get to the user role rather than just using the 'user has role' condition. I think when i had tried previously to use the 'user has role' condition it failed because I made the used 'account:roles' rather than just account.

Hope this helps someone else.

2
  • nice explanation :)
    – arpitr
    Commented Mar 19, 2014 at 14:10
  • Awesome solution, thanks! My users with "Role X" were receiving repeated notifications because I didn't have the "account-unchanged" condition.
    – hockey2112
    Commented Sep 15, 2016 at 15:59

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.