Below is what I started. What can I do to get the View to filter by the taxonomy terms on his/her profile?
Thank you in advance!

Add an argument: Taxonomy: Term ID.
(a) Action to take if argument is not present: Provide default argument.
(b) Default argument type: PHP code1:
$node = node_load(arg(1));
if ($node && $node->taxonomy) {
foreach($node->taxonomy as $term)
{$terms[] = $term->tid;}
return implode(’+’ , $terms);
} else { return; }
This code will return all of the taxonomy terms related to the
current node, and pass them to the view as an argument.
(c) Allow multiple terms per argument.
(d) Reduce duplicates.

Add a second argument: Node ID.
(a) Provide default argument.
(b) Exclude the argument. We’ll exclude this argument, since we only
use it to grab the node ID in our Taxonomy Argument handling
code.

Comments

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

Below is what I started. What can I do to get the View to filter by the taxonomy terms on his/her profile?
Thank you in advance!

By default profiles does not have terms. Do you use content profile or similar?

Bilmar’s picture

Yes, I am using Content Profile. Sorry I did not mention above.

dawehner’s picture

Status: Postponed (maintainer needs more info) » Fixed
<?php

global $user;

$account = $user;

// 'uprofile' is your profiel content type.
$node = content_profile_load('uprofile', $account->uid)
if ($node && $node->taxonomy) {
foreach($node->taxonomy as $term)
{$terms[] = $term->tid;}
return implode(’+’ , $terms);
} else { return; }
?>

This should do it basically. You see what i changed to your code, these are the important lines.

Bilmar’s picture

First and foremost, thank you very much for the quick reply and your support!

If possible – I would like to follow up with a more specific level of filtering I am trying to achieve.

I would like for Views to filter by taxonomy terms selected on current user's profile (content profile) but also factoring in the different vocabularies.

For the example below I have 3 different vocabularies:

Colors
-Red
-Green
-Blue
Size
-Small
-Medium
-Large
Taste
-Sweet
-Bitter
-Plain

If current user selects Red/Small/Sweet (one term from each vocabulary), I would like the View to only show other user's profiles (nodes) that also have all three Red/Small/Sweet.

If current user selects Red/Green/Small/Sweet (2 from color vocabulary, and 1 each from other two) I would like the View to show other user's profiles (nodes) with Red/Small/Sweet or Green/Small/Sweet or Red/Green/Small/Sweet in the results (as Red and Green are from same vocabulary, as long as one is a match on other user's profiles I would like the node to appear in the result, but 'Small' and 'Sweet' must be same as only one term was selected from the vocabularies). If possible, I would like it so at least one term from the vocabulary (if selected) must match.

With the current code above – other user's with Red/Medium/Plain is showing up as well because they share just one common term 'Red'.

Thank you very much in advance!

dawehner’s picture

The interesting thing is that i answered the same question some weeks ago, but i couldn't find it :)



<?php

global $user;

$account = $user;
// the vocabulary id of your specific vocabulary.
$vid = 1;

// 'uprofile' is your profiel content type.
$node = content_profile_load('uprofile', $account->uid)
if ($node && $node->taxonomy) {
  foreach($node->taxonomy as $term) {
    if ($term->vid == $vid) {
      $terms[] = $term->tid;
    }
  }
  return implode(’+’ , $terms);
}
else {
  return;
}
?>
Bilmar’s picture

I apologize for the repeat question - I tried searching under different issue names but couldn't find - as well as google search =)

And again, thank you for your great timing and support!

I have a question - to use the above code for multiple vocabularies, is it best to copy paste the code each time for each vocabulary? or is there a way to include vid=2, vid=3 etc into the code above to minimize any problems?

Thank you!

dawehner’s picture

<?php

global $user;

$account = $user;
// the vocabulary id of your specific vocabulary.
$vids = array(1, 2, 3);

// 'uprofile' is your profiel content type.
$node = content_profile_load('uprofile', $account->uid)
if ($node && $node->taxonomy) {
  foreach($node->taxonomy as $term) {
    if (in_array($vis, $term->vid)) {
      $terms[] = $term->tid;
    }
  }
  return implode(’+’ , $terms);
}
else {
  return;
}
?>
Bilmar’s picture

Hello dereine!

I tried the first code as well as the second and cannot seem to get it to work.

Views Setup
Argument - 'Taxonomy: Term ID'
Action to take if argument is not present - 'Provide default argument'
Default argument type - 'PHP Code'
Code as below (took out as the box instructions says to)
Allow multiple terms per argument - 'checked'
Allow multiple arguments to work together - 'checked'

My content prfoile is called uprofile.
I used a good vid with terms that are common between users but get no results.
I have several fields selected in Views and that is it.

Am I overlooking something very simple?

global $user;

$account = $user;
// the vocabulary id of your specific vocabulary.
$vid = 1;

// 'uprofile' is your profiel content type.
$node = content_profile_load('uprofile', $account->uid)
if ($node && $node->taxonomy) {
foreach($node->taxonomy as $term) {
if ($term->vid == $vid) {
$terms[] = $term->tid;
}
}
return implode(’+’ , $terms);
}
else {
return;
}

dawehner’s picture

could you dsm($terms);
Is it actually set?

Bilmar’s picture

I'm sorry - I don't know what "could you dsm($terms);" is..which I am guessing means that I did not.

I have created vocabularies and terms with taxonomy.
I have checked phpmyadmin and I see the terms and vid=5

Do I require more setup regarding arguments or setup with relationships?

I apologize for the lack of knowledge, but I am very interested!
I would appreciate your support very much =)

Status: Fixed » Closed (fixed)

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

capellic’s picture

@trupal218

I had some trouble getting this going, too. For some reason, the explode statement wasn't returning my TIDs as numbers separated by plus symbols, rather there were 0's where the +'s should have been. But then it magically started working after a recoding the implode() syntax? Very odd.

But then I took a closer look and it the cultprit is the single-quote style that is used in the implode() function. For some reason, in all the code block examples above the single-quote is curled. You want to be sure that the single-quote is STRAIGHT. In PHP, a single-quote means "do not evaluate" and it appears that the curly quote will evaluate the string. In my case (and I suspect yours as well), it's adding nothing against nothing and that's why I was getting 0.

dsm() maps to drupal_show_message(), which when used, will show a warning message at to top of the view (and also in the preview which is *VERY* helpful for debugging). So, If I put drupal_show_message('hello'); in my code, I will see "hello" show up at the top of the view in the message box. I used drupal_show_message() quite a bit to discover what was happening.

I hope that clears things up. The corrected and tested code is below:

global $user;
$account = $user;
$node = content_profile_load('your_content_type', $account->uid);

if ($node && $node->taxonomy) {
  foreach($node->taxonomy as $term) {
    $terms[] = $term->tid;
  }
  return  implode('+', $terms);
} else { 
  return; 
}
andrenoronha’s picture

Thanks for this post

only to complement: the exclude argument must be user id type and with the User ID from URL box checked. and of course the Exclude the argument box checked.

andrenoronha’s picture

just one more thing!

i use user relationships module...
i'd like to exclude from this view the users who are already friend of the logged user...

how can I?

hellomobe’s picture

Any idea how to alter the php code for cck fields?

For example:
Content Node A has a cck taxonomy field with values red, blue, green
User 1 has a cck taxonomy field with a value of red and purple

In views, filter/argument content nodes to only show "red" or "purple" related content nodes when user 1 is logged in. If no one is logged in or there isn't a match don't filter. In this case User 1 and Node A match based on Red.

delvalle’s picture

My use case is exactly the same explained by Bilmar in 4#, but I'm using D7.

Somebody could point me what do I have to change in the code posted in #7 in order to make it work in D7? (apart from changing the curly quotes)

Unfortunately (for me) I don't know PHP, but I guess I should change the content_profile part (using the profile fields available in core or using Profile 2 module) and maybe other elements as well.

I would really appreciate any help (or links to relevant documentation).

delvalle’s picture

For other users in the same situation, please take a look at the solution provided in http://blog.ampli.fi/filtering-a-drupal-7-view-based-on-multiple-user-pr... (works perfectly for me)