On my site, the nodes are restricted based on user's role. Admin selects any number of roles in a field (field_role) when creating a node and if any logged in user's role matches one of the selected roles, he/she can access the node.

But this only works, when user clicks on the node and then either can see the node or see an access denied warning.

The problem is that I just don't wanna show the node, if the access is restricted. The nodes are shown through a view as a listing on home page and I am just not sure how to tell the view that if the logged in user doesn't have any of the role selected in "field_role", then filter out the node from the listing.

Any help.

1 Answer 1


Add field_role in view and hide it.

Now create a custom php filter using Views PHP Filter

Get the current user role and compare it with field_role value. Following is the sample code.

  global $user
  $account = $user->uid;
  $roles = $account->roles;
  $field_role = $data[field_role]
  //$data is available in php views filter
  if(find user_role in field_role) {
    return false;
  else {
    return true;

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.