Dynamic view with a field matching a role of User in Drupal 7 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.
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.

&lt;?php
  global $user
  $account = $user-&gt;uid;
  $roles = $account-&gt;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;
  }
?&gt;