I have been trying to do this for many days but no luck.

On registration, user has a choice to choose his/her interests - checkboxes with term reference widget.
For example - I have made vocabulary 'Interests' with terms sports, computers, painting etc. Every node is related to terms also.

I need to display only the nodes, that users are interested in. For example, if user checked computers on registration, views have to display only nodes with term computer.

I guess there has to be some way to filter only current users profile_field_interests. I couldn't find a way in Drupal 7.

Any suggestions?

Thanks in advance.

Comments

Itangalo’s picture

Hya

You want to (1) use the Views module to create the list – actually a list of users (rather than nodes/content). Then you want to (2) use relationships settings to add in the interests, and finally (3) add a contextual filter on "user: uid" with a default value fetched from the currently logged-in user. Phew!

If you haven't used Views before you in for a ride. If you have used Views but not relationships or arguments/contextual filters, then you're also in for a ride.

There's a pretty nice guide for learning Views in the links in my signature – I hope that can be of help. Look especially for the episode on relationships.

Good luck!
//Johan Falk, NodeOne
**
Learn Drupal with Nodeone! Drupal 7 introduction | The Views module | Drupal learning curve | Rules for Drupal 6 | Theming Drupal 6

minni’s picture

Hey

Thanks a lot! I got it know, I was trying to get to the point from the wrong direction. Yeey!

Cheers
M.

Drupal is always full of suprises
http://www.otsipaku.ee

bfink’s picture

Please explain to me how you did this - I have been pulling my hair out the last 3 days trying to get this to work! Thank you in advance!

Itangalo’s picture

Here is a slightly different approach:

* Create a user view.
* Add a contextual filter on user: uid. If no filter value is present, build a default value using the currently logged in user. (This will give you exactly one user object in your view -- the logged in user.)
* Add a relationship, using the relevant taxonomy reference field on the user accounts. This will bring you information about the "interest" term for the logged in user.
* Add another relationship, called "term: nodes marked with term" (or something like that). This will bring you information about all nodes marked with the interest term for the logged in user.
* Either set the view to display node teasers, or select a number of fields to display.
* Add a display as usual. Done!

Two things to note:
1) I haven't actually done this myself, so I *might* be missing something. It should work, though.
2) "Contextual filters" are called "arguments" in previous versions of Views. Could be good to know if you're not using the latest beta.

Good luck!
//Johan Falk, NodeOne

spcalpo’s picture

Creating a User view instead of Content view is the key.

Thank you, even though it's months later. :)

PascalAnimateur’s picture

Thanks so much for that information, I can now filter nodes based on the user's group memberships :P

Is there a way to display the nodes as rendered entities ? The display style options only suggest "User account" and "Tokens" ... how can I display teasers for the nodes ??

julia_g’s picture

I have the same question, only taxonomy is not involved. I want to filter nodes based on common field in their content type and user profile.
I haven't found any useful relationships either in user view (nothing related to content) or node view (nothing related to current user). What am I missing?
I can probably do this with View PHP, but this really is a basic functionality and should be possible without any additional scripting.

minni’s picture

Hey!

In my case for example, I had a custom field for user. So the field appeared in relationships as User:custom_field. Or it should be available in the relationships for Content: custom_field? Do you have a custom field, which you added both to node and also to the user?

What is the base of referencing? Should the value of the fields be same, or something else?

Drupal is always full of suprises
http://www.otsipaku.ee

bg1’s picture

I have the same problem. I need to filter content based on a value in the content = a value in the user profile of the current user (not author user).

How did you solve this problem in the end?

I am a SQL programmer who knows neither PHP nor the PHP Programming envirnment so I hope you did not have to resort to PHP development for your final solution (I sure wish Views would allow developers to enter native SQL and bypass the query builder part of Views).

Thanks.

gmangones’s picture

Hello, Thanks for that, worked perectly, but i want to add:
After create the view, you must to apply permission to the view.

Gilbert Mangones
Ingeniero de Sistemas
Esp. Construcción de Software

rudyard55’s picture

Thank you so much for pointing out the permissions issue. I spent WAY too much time going back and forth trying to figure out what was wrong and saying "that should work..." and then I saw your post... bingo... started working. Thanks again.

eas_89’s picture

step by step if possible would real help a persons like me

Kristen Pol’s picture

Thanks for this, Itangalo... it works perfectly! Even a year+ later :)

-Kristen
Blog: http://kristen.org
Drupal 7 Multilingual Sites: http://kristen.org/book

enochRoot’s picture

Hi,

Itangalo's approach worked great for me.
But I don't want every content tagged with the term, just certain content-types.
If I add a normal filter, my view returns empty.
and I get this notice:

Handler views_handler_field_node tried to add additional_field language but node could not be added!

why does this handler start looking for a language field when I add a filter for content:type?

help appreciated

thx,
eR

"I have not failed. I`ve just found 10.000 ways that won`t work."

zcrow’s picture

I've spend many an hour trying to figure out how to do this and this worked brilliantly. Thanks.

drtw2ols’s picture

Hi Itangalo,
Your explanation is perfect.
I am a similar problem just a little bit more complex.
I have a content type "Restaurant" with a field restaurant_user referencing the user_name. Then I have Commerce Order with a field order_restaurant referencing the Restaurant. I need to make a view showing to the logged in user only the Commerce Orders having order_restaurant the same as the Restaurant which references the logged in user in restaurant_user.
I am playing with Views but I still get only the Orders made by the user and not the Orders made to the restaurant referencing the user.
How can this be done?
Thanks in advance for anybody helping me to solve this issue!

Adriaan

philipjohn’s picture

Just wanted to say thanks for this. I needed something very similar and these straightforward instructions made it possible. THANKS!

caskraker’s picture

Your recipy works for me, i could not done it without it. One addition: if you have another contextual filter in the view, than the order of the filters also should be considered. In my case i had to put the user: uid filter second otherwise the view generates no output.

Cheers,
Cas

knowledges33ker’s picture

I have a similar question but rather than being interested in the currently logged in user, I am interested in matching and displaying all users who completed a specific field in their user profile.

I have a content type called "idea hub" with a term reference field of interests.

I am using the same term reference field in the user profile where users can indicate issues they are interested in discussing.

I want to create an EVA display attached to the idea hub content type so that when a user visits a page for a given term, s/he will see a view display all users who have indicated an interest in discussing that issue via their user profile page.

So far, I have created a user view and pulled in the fields I want to display. If I use a regular filter and select a single category at a time, I can get a list of users for that term. But I want the filter to be dynamic so that I don't have to build 15 views, one for each category and so that I don't have to build a new view every time we add a new category.

I know that using a contextual filter is the way to go, but I can find the right setting to make it work. I've tried just adding the contextual filter for that field on the user's profile and matching it on the token for the same field in the idea hub content type but I get no results.

I also tried creating a relationship to the user's interests field then adding the contextual filter using that relations. Still, no results. What am I missing here? Any help would be much appreciated.

frozen10’s picture

I understand your steps if using taxonomy reference but I am using an entity reference field in the user profile. they are selecting a field from another content type. I basically want the same thing but i'm using entity reference and can't seem to get it to work and display all content that has the same field that's referenced in the user profile. do you know what relationships would be used for that?

Thanks

mohmmadalabed’s picture

after all this years I would to say thanks man ,,,, it is useful

srikanth.g’s picture

Hi Itangalo,
i have the similar requirement in drupal 6.14:

  • USER1 will access(view/add) only CONTENT TYPE1.
  • Then USER2 will approve(edit/delete) ONLY CONTENT TYPE1. only after user2 approves the content(in CONTENT TYPE1) then only content appears on the site.
  • Both USER1 and USER2 should NOT access any other content types in the website. My site contains more than 10 different content types.

All the links in your signature are dead,plss update them.

maxxer’s picture

I'm about to embark on this. In my case its for Drupal Commerce, and getting "product recommended for you" type thing into the display.

I'm wondering why it is we have to work backwards like this (make a user view, and then search/query for related interesting nodes).

My first instinct was to use a commerce product display view, and then add a User relationship. But of course, in relationships, there are no user terms for a node view, other than user:author, user:comments etc.

I'm going to follow the advice in this thread, but I'm trying to understand views better and was wondering why its been set up like this.

Why can't we retrieve relationships for any entity field from any view? What is the logic behind these limitations? Any insight very very welcome.

kostakis548’s picture

I have the same problem. I need to filter content based on a value in the content can you help me?

ontwerphuis’s picture

Hi I have something similar. I have created online courses that users can purchase and register on checkout - multi user registration allowed but I'd like to limit the selection to only users they have approved relationships with.

I've installed the User Relationship module and have created managers, coworkers etc as relationship types.

I'd like enable managers to select only their coworkers not the entire userlist.

Anyone done this ? Any suggestions?

Thanks in advance.

eas_89’s picture

it seems that these explanation have helped some people out of this problem, un luckly i couldn't make it through following this. can someone please elaborate more on this. i have some knowledge on views, contextual filters and taxonomy terms but still cant catch up with this.

please help
thanks.

alrick’s picture

I've managed to do exactly the same view as the original post describes. I followed the instructions by Itangalo on April 7, 2011 at 5:23am

eas_89’s picture

thanks for encouragement. i have also managed to do it. long live itangalo

kal1982’s picture

Similar problem too. I don't understand what is going on and why we cant fix it!

πλαστική στήθους

Alex Arnaud’s picture

Are you talking about views 3 or an older version ? I have to do the same thing with views 3. And, also, in a view based on search API index but it's an other problem.

I'm stuck at this point:
"Add another relationship, called "term: nodes marked with term" (or something like that). This will bring you information about all nodes marked with the interest term for the logged in user."

Also, i'm wondering how it is possible to show nodes by creating a user view ...

bfr’s picture

It is possible because you are first fetching the user and then all content related to that user. Similary you could fetch content of one type but then actually display content of other type trough relationships. I think the concept of contextual filters was introduced in views 3, so yes, this is views 3.

emag’s picture

You have a user view and want to see that user's nodes. In relationship 'add' the 'user:content authored' . This then opens up content so for example, you can then go back to fields and 'add' 'Content: Title' . You could also go to filter criteria and 'add' 'Content: Type' and then select the content type desired - article, page, blog etc.

I'm still stuck on what others have asked. First so I'm clear, th flag that I'm using is for a node (not user flag type). How do you pull in information from ' flags: user uid's' profile custom fields? I can't get at them. I'm not looking for the author's custom fields but the person who set the flag (node flag) custom fields. I can at least show who that person is by using the relationship 'flags:user' and then adding the field 'flags:user uid' but how get at their custom fields?

Anyone else done this?

guerno’s picture

I needed also a list of nodes where a user is interested in. The information however was in a node type with a term reference field created by the user, and this information should give other node types with the same taxonomy reference field.

I solved this with views php filter, first get the values of the users profile (in my cases I needed some nodes the user created) and then filter with the current row value (you need to included the term id with exluded display, you can do this when going to 'add relationship' and then add the term, after this add the term id as a field and exclude display)

Just give in this code in the php filter in views after installing the views_php module (but change database tables of course to your need)

global $user;
$uid = $user->uid ;

// get all node ID's of the current logged-in user

$result = db_query('SELECT n.nid FROM {node} n WHERE n.type= :type AND n.uid = :uid', array(':uid' => $uid, ':type' => 'xxx'));
$xxx_term_ids_current_user = array();

foreach ($result as $record) {

// search the list of term reference id's of the course

$result_xxx_references = db_query('SELECT c.field_xxx_tid FROM {field_data_field_xxx} c WHERE c.entity_id = :entity_id', array(':entity_id' => $record->nid));

//add the tids to the array with xxx of the user

foreach ($result_xxx_references as $record_xxx_reference) {
array_push($xxx_term_ids_current_user, $record_xxx_reference->field_xxx_tid);
}
}

if (empty($xxx_term_ids_current_user)) {

//if list with interested xxx is empty filter all rows

return true;
}

else {
//variable to determine of there are matching xxx
$matched_xxx=0;

foreach ($xxx_term_ids_current_user as $interested_tid) {

// count the number of matched xxx
if($interested_tid == $row->tid) {

$matched_xxx++;

}//end if

}//end foreach

// if there are now matching xxx, if 0 then the row is filtered
if($matched_xxx==0) {
return false;
} //end if

else {
return true;
}//end else

}//end else

doppel’s picture

This is my way, On views php filter,

//call current user.
global $user;

//load current user custom profile.
$user_profile = user_load($user->uid);
$profile_interest = $user_profile->profile_field_interests['und'][0]['value'];

//Compare views field to current user's custom profile.
if($profile_interest == $row_views-interest){
  return false;
}
else {
  return true;
}

bmango’s picture

Thank you to Guerno and Doppel above. Using your examples, I managed to solve my problem.

I wanted to create a content list of nodes that matched user profile choices. In particular I had two taxonomies that apply to nodes and to users and needed to match these nodes against the user choices. I used a content view and needed to satisfy five different filters. Two of the filters for the taxonomies needed to be dynamic. I needed the filters to be grouped as:

A & B & C & (D or E)

The taxonomy dynamic filters are represented by C and D. I managed to solve my problem by using two contextual filters for D and E. D was a term ID using php code to return corresponding author choices using the code:

global $user;
$account = user_load($user->uid);
$field_items = field_get_items('user', $account, 'field_categories');
//print_r($field_items);
$tids = "";

if ($field_items) {
foreach ($field_items as &$value) {
      $tids .= '' . $value['tid'] . '+';
}
  $tids = substr($tids, 0, -1);
}
return $tids;

I needed to tick the Reduce Duplicates tick box for this filter to make the view use a left join, rather than an inner join, as otherwise I wasn't getting all the results.

The other contextual filter was simply passing a fixed value. I used the Views Contextual Filters OR module to make these two contextual filters use an OR statement. I then used the Views PHP filter module to allow the other dynamic taxonomy filter to be in the static filters section. I tried using the code that Doppel gave, but all the row fields were simply returning the node ID rather than the field values. However, I followed Guerno's idea of adding the term ID's as a relationship and got the following code to work:

//call current user.
global $user;
//load current user custom profile.
$user_profile = user_load($user->uid);

$user_school = $user_profile->field_school['und'][0]['tid'];
$school = $row->tid;

//Compare views field to current user's custom profile.
if($user_school == $school){
  return false;
}
else {
  return true;
}

Using this configuration I managed to get the filters working in the way I needed.

Jan_MSK’s picture

Creating a user view instead of a content view is indeed the key.

However, now that I have created a view that filters exactly the things I need, i cannot select it to use it in a node refencere field.

Here's what I need: In node type A, I want to reference nodes of another type B, but I want each user to be only able to reference B type nodes in which users of their own affiliation are referenced. Therefor I created a view like suggested in this thread: not a content view, but a user view constructed with some relationships to get the right results.

So far so good, the reference display of my view is working smoothly. The only problem is: it is not showing up in the field settings of the node reference field of node type A. My guess is only content views are shown here...

Is this correct? And how do I fix it? Thanks!

pinueve’s picture

Hi Jan_MSK, I'm in the same situation, my node reference view displays all as needed (users view, uid contextual filter), but on reference node form is only showing the last row, could you solve it?