1

I have a view that lists terms from two different vocabularies (Product Categories, and Traits). The Product Categories vocabulary looks something like this:

  • Sweet Corn
    • Type 1
    • Type 2
  • Onions
    • Type 3
    • Type 4
  • Popcorn
    • Type 5
    • Type 6

Each of these parent categories also have specific traits that are identified in the Traits vocabulary that looks like this:

  • High Expansion
  • Augmented
  • Celebrate

High Expansion is a trait of Popcorn, Augmented is a trait of Sweet Corn, and Celebrate is a trait of Onions. Traits are different from Categories, and must be defined in a separate vocabulary due to other definitions and functionalities on the site. So, I can't just make them children of the above parent terms.

I have a view that is a main landing page for the product categories that displays the icons for all the Categories and Traits, that the user can click to see a filtered view of products within the category clicked. I am grouping the view results by Parent term, but also need to include the Traits in the appropriate groups.

On my landing page view, I need to list the Traits under the appropriate grouping, so my landing page view should look something like:

  • Sweet Corn
    • Type 1
    • Type 2
    • Augmented
  • Onions
    • Type 3
    • Type 4
    • Celebrate
  • Popcorn
    • Type 5
    • Type 6
    • High Expansion

Is there any way to accomplish this type of grouping?

Thank you for your help!

6
  • I'm very confused by your question. Please identify all of the entities that we're dealing with, and how they relate to each other. It sounds like you have 2 separate vocabularies: Product Categories and Traits. It also sound like maybe you have a content type called "Product" -- is that true? What relationship does each item have to the other? Furthermore, we need more of an explanation of what you're actually trying to list in Views.
    – Aaron
    Commented Aug 28, 2015 at 21:14
  • Sorry for the confusing explanation, I have edited my question to try and clarify. Let me know if this helps. It's a tough one to explain in writing. Commented Aug 31, 2015 at 16:37
  • Much better! Thanks for the improved question. It is very clear now.
    – Aaron
    Commented Aug 31, 2015 at 16:49
  • That is a different, and better, question. You say "Each of these parent categories also have specific traits that are identified in the Traits vocabulary" do you mean the Trait term is on the Category term, or is it on a product that is marked with the category parent? Commented Aug 31, 2015 at 17:59
  • The trait term is on a product that is marked with a category child. Commented Aug 31, 2015 at 19:57

2 Answers 2

0

For this to work "properly" (I say properly because you could fudge it visually with CSS and javascript but let's not do that) without custom code you're going to need to do one of 2 things:

  1. Make the "Trait" terms reference the "Category" terms rather than the other way around ...or:
  2. Make the "Trait" field on the "Category" terms be an entityreference field rather than a term reference field. This is necessary because entityreference gives you views relationships that let you access the referencing entity.

I'll assume you went with option #2 for the purposes of this write-up. The solution will be quite similar if you went with option #1, but you just won't need to add a relationship in the first view we create, below.

Once you've done that, create 2 views:

  1. One that can be used with the Views Field View module (could also be done with the EVA module in a similar fashion) to list the items that belong under the top-level categories.
  2. One for listing the top-level categories

View #1:

This one is the more complicated view. This view is going to take in a term ID of one of the parent categories (like the tid of "Sweet Corn") and we want it to list any terms that are either child terms of that term, or are traits of that term.

Install the Views Field View module and the Views Contextual Filters Or module. This view should also be a list of taxonomy terms and needs to be configured like so:

Relationship: Add a relationship to the referencing entity via the traits field.

Contextual Filters:

  1. A contextual filter on the parent term ID.
  2. A contextual filter on the term ID (be sure to set this to use the relationship to the referencing entity that we already set up)

Now, we need to make our contextual filters use "or" logic, so if you installed the Views Contextual Filters Or module you will now have an option in your view under Query Settings to turn this on.

View #2:

This should be pretty straightforward. This will also be a view based on taxonomy terms. You'll probably want to make this use a "page" display and give it a URL. And you'll want to choose to render the terms using "fields".

Fields:

  • Term name
  • Term ID - set this to be excluded from display, we only add it to make it available as a token for our next field
  • Global: View -- you'll be able to select the appropriate view and display that you configured when you set up view #1, above. You'll also get to enter arguments to pass to the view, so enter [!tid]/[!tid]

Filters: (just regular filters, not contextual filters)

  • Vocabulary (so that it only shows terms from the "Category" vocab.
  • Parent term. Set this filter to be equal to 0. This will ensure only top-level terms are displayed.
1
  • This worked perfectly! I'm not sure I understand how it's working, but it works. Thank you for your help! Commented Sep 1, 2015 at 17:59
1

The views Format Settings has your answer. There is a drop-down for "Grouping field Nr.1" that does exactly this.

enter image description here

As it is, you have one "row" showing for each unique set of values because this is what mysql returns. By setting this field grouping, Views will match up the values from the selected field and use them as headers for sub sections of your results.

If this is not a good enough match to your issue, please provide details on how it doesn't work and I can update the answer.


Continued:

The view is a tool for making a mysql query and then theming the returned result. With this in mind, you need to craft a set of data relations and filters to get the appropriate data linkages for the ratios you want in your display. This means you want the database handing you a field table like:

Category parent | product | category | trait

  1. Sweet Corn | corn product 1 | Type 1 | Null
  2. Sweet Corn | corn product 2 | Type 2 | Null
  3. Sweet Corn | generic corn product | Null | Augmented
  4. Onions | onion product 1 | Type 3 | Null
  5. Onions | onion product 3 | Null | Celebrate

Then once you have the dataset you need, you work on the theming side of the views tool. Use the above mentioned Grouping field setting on the Category Parent. This will make the sections with the parent titles as indicated in your question, but it might look like

  • Sweet Corn
    • corn product 1 | Type 1 |
    • generic corn product | | Augmented

Then use the "exclude from display" on the product field, or actually remove it from the fields list. I have it included here because it is needed to show the data grouping as I understand it in your question. Finally, configure the "category" field with the link you want it to have and exclude it from display; use the "No Results Behavior" rewrite option on Trait to put in the token for the Category field, something like [field_term_category]. You should be able to find this token in the list of "replacement patterns" available in the Rewrite Results area.

enter image description here

You should now have a list of parent categories showing the linked sub categories and traits where you want them. The "trait" field will either have the category or the trait in there, as it shows trait when trait is present, and category when not. Be sure to uncheck the "hide if empty".

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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