0

I am trying to hide the results of a view from users who have a specific permission. For instance, Let's assume that a user has the permission "create page content". I would like for the view to show NO results to this user.

I can't restrict on roles because the user can have overlapping roles. Basically, I need the "Access" portion of views to work opposite of how it normally does.

I suspect I should be able to do this with the Global:Null argument validator, but I am having no luck. However I am assuming that has more to do with me weak php chops.

Thanks in advance.

1

Hook_perm to the rescue! I would suggest thinking the other way around: Grant access to the view's results if the user HAS a permission. Create a new permission and assign it only to the roles that may view the results.

In your custom module, add a new permission like so:

function mymodule_perm() {
    return array('can view results of my view');
}

Then in your view, change the 'Access' setting to your new permission.

You may run into problems if the roles that you're trying to exclude have the 'Access all views' permission (or a similar administrative views permission). If that is the case, you'll need to use a views hook to remove the results.

function mymodule_views_pre_render(&$view) {
  switch($view->name) {
    case 'the_view_name':
      if(!user_access('can view results of my view')) {
        $view->result = array();
      }
    break;
  }
}

I would flush caches in order to have this hook picked up by views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.