1

I'm on drupal 7, with views 3.5.

I have added to the user profile, a taxonomy field.

I want to create a view, which will get the users which that taxonomy field has the same value with the current logged in user taxonomy field value.

Is it possible?

  • use contextual filters. – monymirza Mar 1 '13 at 11:57
  • Well this is where I'm trying to get this working. But on "provide default value" there is no option to get the current users taxonomy field. Only uid. – Alkiviadis Doulgeris Mar 1 '13 at 12:21
1

Suppose your taxonomy field is labelled User Tag and has a machine name field_user_tag.

  1. Create a view that lists users

  2. Add a contextual filter for User: User tag (field_user_tag)

  3. Under WHEN THE FILTER VALUE IS NOT IN THE URL Select 'Provide a Default Value' and then Select 'PHP Code'

  4. Insert the following snippet:

    global $user;
    $term= user_load($user->uid)->field_user_tag;
    return $term['und'][0]['tid'];
    

The PHP code grabs the current user. In this context the current user object doesn't have any additional fields loaded. So we need the user_load function to get those. The we need to extract the taxonomy id from the relevant array.

This approach works, but may not be the best or most efficient.

  • Please what of if the field is a 'Select list field' created uding "Profile 2 module" ? – seyi Nov 2 '14 at 4:14
  • Perhaps this solution works in some use cases, it didn't in mine even though it seemed to match exactly. I'm not downvoting or saying this isn't a valid solution, but if this doesn't help you, try this solution. – Travis Sep 19 '16 at 16:43

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.