Display only parent terms in the View

Submitted by ravisagar on Mon, 06/08/2015 - 02:59

On one of our website for our client, we have used one taxonomy for all the states in India and each state further has various districts. These districts are added as the child term. This taxonomy is used in one of their content type, now few days back our client wanted to see a simple tabular report in which a count of all the nodes for each state can be viewed in real time.

Now this report can be esily created using viewbut when you use the same taxonomy term which has both parent and child then all the terms will be listed, so we were able to generate the report with all the state and their districts listed in the same table.

Our requirements was simply to display the list of all the states, that is only the parent taxonomy term. We did the following steps in the view to generate such report. It is pretty easy but not many people know how to do it.

Follow these steps

1. Add the Relationship in your view: "Taxonomy terms on node".
2. Select the vocabulary you want to use in this Relationship.
3. Name this first Relationship as "term".
4. Add second Relationship: "Taxonomy term: Parent term" and use the Relationship "term" from step 1. You wont get the Relationship "Taxonomy term: Parent term" until you add relationship "Taxonomy terms on node".
5. Name this second Relationship as "parent"
6. Then add field of type "Taxonomy term: Name" and use "parent" relationship that you added in step 4.

That is the power of Drupal's site building. It is convenient to use Views module along with the relationships.