Please find my problem statement here:

Show all contents of Type "Test" belongings to that country to corresponding user of that country

My Approach:

  1. Created a taxonomy term of country, Mapped this taxonomy in content type "Test" and also Mapped taxonomy to users account setting fields.
  2. Trying to create view that can filter based on logged in user's country(user can belongs to more than one country) to the content's country field.

Trying with Contextual filter, but not figuring out, exactly what i need to give. Please anyone can help me in achieving this?

Or suggest me any other Better Approach

up vote 1 down vote accepted

What you're missing is the bridge between users and content, and that comes through Relationships (connecting different tables from the database).

You can do this by approaching from either the content end, or the user end (Content Views or User Views type). Since the goal is to list content, I think it makes more sense to start from the content end, unless you need some more user handling that would be easier from the user then.

  1. Create a Views showing your Content (Page or a Block, which ever suits you best)
  2. Add a Relationship Taxonomy term referenced from field_country (you probably have a different field name), you can require it since you only want nodes in the list that don't have Country empty
  3. Add another Relationship User using field_user_country (you probably have a different field name), it will use the above Relationship by default. You can require it since you need users who also have the Country field not empty.
  4. Add a Contextual filter ID from User Category and it will use the second Relationship by default. Configure it:
    When the filter value is NOT available
    Provide default value
    User ID from logged in user
    When the filter value IS available or a default is provided
    Specify validation criteria
    User ID
    Single ID
    Hide view
  5. Adjust any other Views settings relevant to you and Save the View
  6. If you've created a Block and need to place it in a Region in your theme, don't forget to adjust the Contextual Views options on the Block configure page.
  • Super.... Thank you so much... It is working.. but i didn't get, 'Hide view' option ,which you have mentioned in 4th point. In 'Action to take if filter value does not validate' , i selected display contents of no result found. – rks Nov 28 at 4:49
  • For full website, for every view i need this kind of filter, is it good to have contexual filter in every view. Will it effect to Website performance.. Is there any better approach? – rks Nov 28 at 10:19
  • I don't know why you don't have the "Hide view" option, but that is just the case if the argument isn't valid, so you can adjust that to how you like it. I can't imagine scenarios when there wouldn't be a user ID when someone is viewing the website (apart from anonymous) so there will always be a valid argument. Use view permissions to hide the view from Anonymous visitors. Performance: if you know SQL then you may optimize the database query and make a faster one than Views. I'm not sure if that would help a lot, I'm not an expert. Usually you need to use caching to speed things up. – prkos Nov 28 at 16:47
  • ok.thank you so much..yes view permission is set for only 'Authenticated User' – rks Nov 29 at 4:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.