Search code examples
drupaldrupal-7drupal-modulesdrupal-views

View + Groub by + taxonomy + level one


I use drupal 7.18. I created a content type 'company' with term reference field 'location'. The taxonomy 'location' has tow levels 'continent' and 'country'. I went to list all 'company' node grouped by 'continent'(the top level of the taxonomy 'location').

I question if this option is available in the views module or i should alter the query?

thanks


Solution

  • Yes, you can actually do that with views without any custom coding.

    Scenario 1

    It would be easiest if you tag your company nodes with both the continent and country terms when you tag them. If that is the case, you can simply add the term in the field list of your view and then group by that field.
    Once you've added the term as a field, click on the 'Settings' link next to your view's format. There you will see an option to 'Group by' any of the fields you've added to your view. It may then be repetitive to also display the continent, so you can just select to exclude it from being displayed.

    Scenario 2

    It is also possible that you select the country tag (child term) without selecting the continent term (parent) in the country nodes. If that's the case, you won't be able to add the continent as a field and then group by it (since it isn't selected for the node, it isn't a field for that node). In that case you need to add two relationships to your view (Relationships are added in the 'Advanced' section of your view) first to get to the parent term.

    • Add 'Content: location' as your first relationship (and you can check that it should be required if you only want to list companies that have been tagged with this in your view).

    • Next add the relationship 'Taxonomy Term: Parent term' and make sure to use the 'Content:location' relationship when you set this up. That will pull up the parent term of the tagged location automatically.

    • Next add the 'Term: Name' field in your list of fields, and at the top select to use the 'Parent term' relationship you set up previously.

    • Finally, now that you have the parent term printed out in your view, you can group by it the same way as I described earlier -- by clicking on 'Settings' next to your format and selecting this field as the one you should group by.

    I hope I explained this clearly. Let us know if it works for you or if you have questions!