Drupal Views using contextual filters with multiple taxonomy vocabularies

In Drupal, out of all the modules available for all kinds of tasks, there are certain modules; that no matter what kind of project we are working on; we are always using them. A very good example of this kind of module is the Views Module, a module so important that is going to be included in the Drupal Core Version 8.

The degree of difficulty of creation of a view is always relative, and it will depend on the complexity of the data that we want  to show on the view.

The problem

In this case, we have a "content type" and 3 different taxonomy vocabularies using 2 levels of hierarchy. The 3 types of taxonomies can be applied to a single node.
The vocabularies are used as menus to filter the results.

Taxonomy Vocuabulary 1

  • Term 1 (parent)
    • Term 3 (child)
    • Term 4 (child)
  • Term 2 (parent)
    • Term 5 (child)
    • Term 6 (child)

Taxonomy Vocuabulary 2

  • Term 7 (parent)
    • Term 9 (child)
    • Term 10 (child)
  • Term 8 (parent)
    • Term 11 (child)
    • Term 12 (child)

Taxonomy Vocuabulary 3

  • Term 13 (parent)
    • Term 15 (child)
    • Term 16 (child)
  • Term 14 (parent)
    • Term 17 (child)
    • Term 18 (child)

Therefore when it’s time to create a node, it may have 1 or various terms related to the node and those same terms could repeat in 1 or various nodes of the same type of content.

  • Node 1
    • Term 4,
    • Term 18
    • Term 10
  • Node 2
    • Term 12
    • Term 3
  • Node 3
    • Term 18
    • Term 9
    • Term 6
  • Node 4
    • Term 10
    • Term 3
    • Term 6
    • Tem 17
    • Term 18
  • Node 5
    • Term 5
    • Term 11
    • Term 15
    • Term 12

 

Now, what we want to accomplish in this case, is to show certain fields of the content, among those, the title  and the terms of the vocabulary 1, and regroup them according the parents of the vocabulary 2, but when you access the view to be able to view the nodes according the vocabulary and the term that I clicked on.

In order for this to happen we are going to have to add a relation according the vocabulary that we want to regroup, and after that we add a second relation that asks for the father of that same term.

So far there’s nothing different with other common views, however the challenge in this example is that it should only display the terms according to the term we have consulted.

Image
relationship

The url will always be view/parent/child depending on the term we consulted, therefore we should add the % to the path option, that will indicate the position of the contextual filters.

Image
page

When adding the contextual filters we will be tempted to add the father as the first filter, however we do not need it , because if we check the tables on the data base for the taxonomies, the data base will save the hierarchy of each term.

Image
taxonomy

Since these data is already on the data base, we do not need it, therefore the first filter will be Global:null

Image
global

For our second filter we will use the name that comes in the field # 3 of our URL view/parent/>child< and we will use the filter Content: Has taxonomy term ID and we are going to tell him to look for in the URL

Image
contextual

Now it may happen in some cases, that the consultation goes wrong due to the fact that in this case there are some terms that have the same name, therefore  in order for us to prevent this from happening, we will tell him to replace the name of the URL for the ID

Image
filter

 

Image
filter

And with this we can already filter the tid according to the URL, and that it doesn’t consult by the given name the URL.