Tutorial :Check views display permission for user


A user might be in role X.

There exist a view, where display A is allowed for role X while display B is restricted.

How do i programmatically check whether a user belonging to role X can access the display or not?


What you should do, is to check the permission instead of the role using: user_access


Is there a specific reason why you want to do this programmatically? You can set access rules for Views displays in the Views UI:

Edit the view, select the display and look for "Access" in the "Basic settings" block. Click the value (default = "Untrestricted"), click the "Override" button to override the setting for that specific display and choose the settings you need.


Can be implemented inline in the theme, but better to break it up into module + theme. (assumes drupal-7) In your theme (node--contenttype.tpl.php) invoke a custom access method:

if (module_invoke('hottopicresearch', 'display_moderated_research_access_callback', 'update', $node)) {  

Implement an this access callback in a module:

function hottopicsresearch_display_moderated_research_access_callback($permission, $node) {  

And check roles

 if (in_array("editorial board admin", $user->roles) || $user->uid == 1) {  

and/or node access as noted in other answers:

 if (!node_access($permission, $research_parent_node)) {  

returning TRUE or FALSE.

This example gave access to people with 'editorial board admin' role and people who can write to the node. Nobody else can see the index. Of course this doesn't stop them accessing the node directly.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »