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
Comment #1
rdeboerhttp://drupal.org/node/1490810#comment-6855988
Comment #2
delvalle commentedThanks 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?
Comment #3
delvalle commentedAnyone?
Comment #4
joachim commentedYou 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.
Comment #5
delvalle commentedYes, 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.
Comment #6
joachim commentedAdding several arguments on different fields should also work fine.
Comment #7
delvalle commentedThanks 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.
Comment #8
kaizerking commented@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
Comment #9
rdeboerGoing back to the code in the original issue....
Instead of
Try something like this:
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!
Comment #10
kaizerking commentedThank 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
Comment #11
kaizerking commented@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
to
The content is filtered according to field_ and filed_y
how to do this for field collection fields? any help please
Comment #12
rdeboerHi 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
Comment #13
kaizerking commentedRik , pl have a look here field extractor feature in 'When the filter value is NOT in the URL'
Regds
kaizerking
Comment #14
rdeboer@kaizerking
Interesting module. Didn't know about that one.
Thanks for bringing it to my attention
Rik
Comment #15
kaizerking commentedFor 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
Comment #16
rdeboer@kaizerking, #15:
Great! Well I guess that means that between all of us we've pretty much solved this issue!
Rik
Comment #17.0
(not verified) commentedhighlight code tags