-1

I have a files listing view which I need to filter on the basis of current logged in user's roles. Since one user can have multiple roles I want to filter data on the basis of all the roles those are assigned to current logged in user. I used following code but it didn't work.

How do I achieve this?

here is the code that I wrote:

function ModuleName_views_query_alter(&$view, &$query){
  global $user;
  if ($view->name == 'my_view_name'){
    $user_roles = $user->roles;
    $view->query->add_where($user->roles,$user_roles,'IN');
  }
}
2

You can use something like this.

function ModuleName_views_query_alter(&$view, &$query){
  if ($view->name == "my_view_name") {
     $join = new views_join;
     $join->construct('TABLE NAME', 'NODE', 'nid', 'entity_id', '', 'LEFT');

     $view->query->add_relationship('TABLE NAME', $join, 'node');
     $query->add_where('TABLE NAME', 'TABLE NAME.FIELD NAME', FILTER VALUE, 'IN');
  }

You Can Use your entity type in this function, i used NODE here. I hope this helps you.

Don't forget to mark it right if this help you ;)

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.