1

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->roles))) {
        $query= $_GET['query'];
        return $query;
      }
    else{
        $user_uid = user_load($user->uid);
        $myfilter = $user_uid->field_custom['und']['0']['target_id'];
        return $myfilter;
    }

The else filter works great but it is failing to filter for the administrator role.

0

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->uid);
  $myfilter = $account->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.

  • that works great, I modified it slightly to use the url alias instead of the query string – Stuart May 22 '15 at 12:20

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.