php in views contextual filter for different user roles I am attempting to filter a view based on a php contextual filter, however the filter needs to come from different sources depending on the users role:

administrators - from a query string
all other roles - from an entity reference field on the user

I am using draggable views which prevents me from using two separate views.

So far I have:

global $user;

    if (in_array('administrator', array_values($user-&gt;roles))) {
        $query= $_GET['query'];
        return $query;
      }
    else{
        $user_uid = user_load($user-&gt;uid);
        $myfilter = $user_uid-&gt;field_custom['und']['0']['target_id'];
        return $myfilter;
    }


The else filter works great but it is failing to filter for the administrator role.
You could also use user_access() to see if a user has the rights do to something.
If you use some core administrative tasks, you can be sure that it will trigger only for administrators

E.g.

global $user;
if (user_access('administer site configuration')) {
  $query = $_GET['query'];
  return $query;
}
else{
  $account = user_load($user-&gt;uid);
  $myfilter = $account-&gt;field_custom['und']['0']['target_id'];
  return $myfilter;
}


Note: I used $account instead of $user_uid to store full user object; most of the contributed modules and core name the variable this way.