How to check if a user has an access to a certain page in Drupal?

I want to know how to see if a user can access certain page in Drupal

Leo Barber Asked on October 21, 2015 in Drupal.
Add Comment
1 Answer(s)
Best answer

To see if a logged in user has access to a page on the site, you can use the code below:

if ($router_item = menu_get_item($path)) {

if ($router_item[‘access’]) {

// The user has access to the page in $path.

}

}

The $path variable is the path to the node you want to check. For example it can be node/1, admin/user/user or other existing for the Drupal site.

Now if you want to check the same for an user who is not currently logged to the site, then you will have to alter the global $user variable first. You can check the following link on how to do that. The only difference in that case is that you will be using the value returned from user_load() instead from drupal_anonymous_user()

Kevin Cane Answered on October 21, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.