How can I check if a user has a certain role?
I found this but it is for Drupal 6.
|
|
|
Since you already found that post, make sure you also read the comments. It clearly explains why checking for a permission is recommended over checking for a role. When you use permissions, you can assign that permission to multiple roles, which makes your system more flexible. Also, remember that roles can be renamed, which would break your code. That said, if you want to check for a role, you can do this:
|
|||||
|
|
You could install devel module and do dpm($user). This will print an array with all user information including the user role. From this array you could find the array position of "roles" and use it in your module to find the user role. |
|||
|
|
|
You can check the role of a user by print_r($user) and in output you will get something like this
stdClass Object
(
[uid] => 0
[hostname] => ::1
[roles] => Array
(
[1] => anonymous user // Current user role mine is anonymous in your case it may be different
)
|
||||
|
|