drupal rules user account unchanged

914 views Asked by At

I'm using Drupal 7 + Rules 2 and would like to create a rule that shows the user a message when their role has changed from one role to another. I was experimenting with using "account-unchanged" in a data comparison condition, but wasn't having any luck. My configuration is below, however, using the "unchanged" feature is foreign to me, and I'm not sure I'm on the right track.

  • EVENT: After updating an existing user account
  • CONDITION:
    • Data Comparison - account-unchanged:roles equals RoleA
    • Data Comparison - account:roles equals RoleB
  • ACTION: Show a message on the site=Role changed!!!

As stated before, this produced no results when updating a user who had RoleA to having RoleB, and in the Rules debugger, the conditions are always evaluated to be FALSE.

If there is an alternate/better way of accomplishing this than the above, I would appreciate being switched to the right track :)

Thanks so much!

2

There are 2 answers

0
areikiera On BEST ANSWER

Was able to answer my own question!

  • EVENT: After updating an existing user account
  • CONDITION:
    • Data > List contains item - LIST: account-unchanged:roles, ITEM RoleA
    • Data > List contains item - LIST: account:roles, ITEM RoleB
  • ACTION: Show a message on the site=Role changed!!!

If you specifically want a message when a role was changed from one to the other, and not simply added, add another condition but NEGATE it. In my example, I would add:

  • Data > List contains item - LIST: account-unchanged:roles, ITEM RoleB (Check NEGATE)
  • Data > List contains item - LIST: account:roles, ITEM RoleA (Check NEGATE)

See https://groups.drupal.org/node/94379#comment-631148 for reference. I had been focusing on the VBO answer there and used this comment in conjunction with that approach and still had trouble. Just now thought to look at it on its own... and voila!

Hope this helps somebody!

0
Michael Hall On

In addition to arikiera's answer, if you would like to check if a user has had a role revoked you can use:

  • Data > List contains item - LIST: account-unchanged:roles, ITEM RoleA
  • Data > List contains item - LIST: account:roles, ITEM RoleB (Check NEGATE)

I used this to add some rules to the subuser module which will remove a specific role from a parent user's sub users if the parent user has their role revoked. This is very useful if you are building a site that has corporate accounts who can add their own sub users.