Hi,
My apologies if this has been answered but I have searched posts a lot and tried to figure this out in many different ways and have not been able to.
As an example, this is what I would like to do:
All users have a custom profile field called "Occupation".
When a user opens a View, I would like to have this view display the nodes where the Occupation of the authors of the nodes is the same as the occupation of the current user that is using the View.
I have tried Arguments have not been able to set an argument to the author's occupation.
I have tried to create a Rule where the rule sets a CCK field in the node to the author's occupation.
I have tried to create a Relationship but can't seem to access the author's occupation.
Any help would be much appreciated. Thanks!
Comments
Comment #1
merlinofchaos commentedThe only way to accomplish this right now would be to use the PHP Code default argument and write a piece of PHP that woudl get the current user and extract the appropriate profile field. Views would then see that field as the argument, which you would match up to the same profile field.
SO let's say you have a profile field named foo:
1) Add the Profile: foo argument
2) Set the argument default to "Provide default argument" and select PHP code (alternatively, learn enough Views API to write an argument default plugin so you don't embed PHP code into your site). Then use that PHP code to do a global $user and run whatever PHP is needed to get that profile field from the user (it's been years since I had to do this, so I don't remember how).
3) return that value.
Comment #2
druppeler commentedDear merlinofchaos,
btw I think you have created a great piece of software that benefits many many people.
Thank you for personally responding to my issue and laying out the solution.
I did see where I could use the PHP Code default argument - I don't know how to write the code but I will endevour to learn this on my own (without posting more support requests).
Comment #3
merlinofchaos commentedThere should be several examples of doing stuff like this in the handbook, and people on the forum might be able to help with smallish snippets like this.
Comment #4
druppeler commentedDone! I am now able to get the current user's Occupation into the Argument.
For the sake of anyone who may have the same issue in the future, pending further testing, the code is:
This produces the following query:
Where "Accountant" is the correct value of the Occupation field of the current user.
As merlinofchaos suggested, I searched drupal.org forums, handbooks, also using Google and tried many different ways until I found the way that worked. The ways that I tried include:
Thanks merlinofchaos!
Comment #5
druppeler commentedComment #7
Yanxi commentedHi druppeler, thanks for sharing the code. I've been searching for a solution for two days and was very happy when I saw this thread. But unfortunately it didn't work in my situation. I am using Content Profile module and not sure if that's the reason. Are you using the native profile module in Drupal?
I created a page view with a path of mysite/myspace and gave it a primary link on the front page, hoping when user A clicks it it would take the user to mysite/myspace/userA-content-profile-field-value page. I have tried lots of other codes suggested by other threads without any success. A couple of other codes I have tried in the default argument php field are these:
Any thoughts would be appreciated.