Hi, I'm trying to apply a contextual filter in a view, according to the taxonomy term selected by each user in a field of their profile.
This can be done with core profile fields following the process explained here (I've tested it and works), but I don't know how to do the same with Profile2 fields.

As you can see in that link, the code that works for core profile fields is:

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

I've tried adapting that code to Profile2 after reading some related issues like this and this and also installed devel to see if I can get the info, but I don't know PHP, so I'm completely lost here. The last PHP code that I tried was the following:

global $user;
$uid = user_load($user->uid);
$profile = profile2_load_by_user($uid, 'myprofile2');
return $profile->field_colors['und'][0]['value'];

I would really appreciate if someone could provide the right code and/or point me to the relevant documentation (hopefully something understandable by a noob like me).

Comments

rdeboer’s picture

delvalle’s picture

Thanks Rik, but if possible I would like to achieve a views-only solution (with no additional modules required). If that's not possible, I will surely use your fantastic Global Filter module.

Any suggestions for the PHP code?

delvalle’s picture

Anyone?

joachim’s picture

Status: Active » Fixed

You would be much better off having a regular 'current user' Views argument, and then using Views relationships to hop to the colour field from that.

delvalle’s picture

Status: Fixed » Active

Yes, that would do the trick for 1 field (following the steps explained here), but I'm trying to do it for several fields simultaneously and Views relationships doesn't seem to work for this (at least I was unable to make it work, and I've done quite a lot of testing, searching and asking...)

The code provided in this post works perfectly for this use case using core profile fields. I'm trying to do exactly the same but with Profile2 fields.

Sorry, joachim, I should have explained my use case on more detail.

joachim’s picture

Adding several arguments on different fields should also work fine.

delvalle’s picture

Thanks for following up, joachim. Could you please elaborate your answer a little bit more?

The solution that I'm trying to implement (explained in the abovementioned post) uses several arguments indeed (one for each field), but in order to "match" the fields, PHP code (as a default value in each argument) seems to be the only solution.

As I've said in the previous message, the solution from that post works perfectly for core profile fields (using several arguments with PHP code), but I need the same for Profile2 fields.

Are you suggesting that there is another way to use arguments for this use-case that doesn't require any PHP code?
If so, that would be the perfect solution. If not, I would really need some help with the PHP code for Profile2 fields.

kaizerking’s picture

@xiwino have you solved this issue?
i also have same requirement,
I want to filter nodes based on profile2 term reference
I have three term reference fields (one of them is in field collection field)
I want to create a list of nodes in table view where ALL the three terms match
I am not finding a way,
if you have solution please share

rdeboer’s picture

Going back to the code in the original issue....
Instead of

global $user;
$uid = user_load($user->uid);
$profile = profile2_load_by_user($uid, 'myprofile2');
return $profile->field_colors['und'][0]['value'];

Try something like this:

   
    // The first occurrence of field_name, on either one of their profile2 or core
    // user profiles will be used to return field values from.
    global $user;
    $profiles = profile2_load_by_user($user);
    foreach ($profiles as $profile) {
      if ($items = field_get_items('profile2', $profile, 'field_colors')) {
        break;
      }
    }
    // depending on the nature of the field, this last line may have be adjusted a bit....
    return $items[0]['value']; 
 

Taken from the http://drupal.org/project/global_filter module, which is probably not a bad solution for what you're trying to do... without coding!

kaizerking’s picture

Thank for this help,
as i explained in my comment I have three term references fields field_x, field_y,field_z
two are directly in profile2(field_x, and field_y) and one is (field_z) on field collection field which is attached to profile2
I don't see iteration on field collection field in the code
I wanted the three should match with terms on node which has same field_x,field_y,field_z.
if all terms match then the node will be listed in the tables along with other fields on the node
thank for trying to help

kaizerking’s picture

@RdeBoer thanks it worked for profile2 fields
I just selected fields in the contextual filter and selected provide default values and used php code
changed your code from

 // depending on the nature of the field, this last line may have be adjusted a bit....
    return $items[0]['value'];

to

 // depending on the nature of the field, this last line may have be adjusted a bit....
    return $items[0]['tid']; 

The content is filtered according to field_ and filed_y

how to do this for field collection fields? any help please

rdeboer’s picture

Hi kaizerking,

My code in #9 was more in response to the original request by xiwino for fields in general -- nothing to do with the field collections in your reply.

You are absolutely right that for TAXONOMY fields 'value' needs to be replaced by 'tid'.

If I have time I may have a look at how to extract values in field collections...

Rik

kaizerking’s picture

rdeboer’s picture

@kaizerking
Interesting module. Didn't know about that one.
Thanks for bringing it to my attention
Rik

kaizerking’s picture

For field collections i did not find any way so I used flag to flag the term by user when ever user creates(adds term ) new->flag , on update> unflag -> unchanged and flag-> changed and unflag on delete the term rule
added flag relation in views and set show only flagged terms by current user.
This along with the php code mentioned above solved as work around for me
just as info if any one is looking for same

rdeboer’s picture

Status: Active » Fixed

@kaizerking, #15:

Great! Well I guess that means that between all of us we've pretty much solved this issue!
Rik

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

highlight code tags