Hierarchic menu in Drupal with tags & Views

Submitted by Nicola Rainiero on 2012-12-19

In this article I will explain you how to use relationship tags, in order to create a block with Views that contains a hierarchic menu. I also made a custom module to show only the correct localized and language neutral tags.

In the previous one, called Tag Cloud in Drupal with Views & Taxonomy, I introduced the meaning of taxonomy and its implementation in Drupal. Today I will add a new feature useful for the description below: RELATIONS (parent --> child) of the tags. You can do it linking here: http://mio_sito/admin/structure/taxonomy/tags. It's simple: select the tag that you want make child and move it into the one that becames parent. At the end save it all!

Hierarchic menu

I post my list of default terms:

List of default terms in Drupal

Here is the final menu for the Italian and English section:

Menu in ItalianMenu in English
menu in Italianmenu in English

As you can see, in these menus there are only the relationship tags, the others don't appear. For example the article is tagged with: web, drupal, views e modules. So it appears only if you click on web or drupal menu items.

Views settings in Drupal

In the same way that I explained in Tag Cloud article, you have to define a new view from here: http://my_site/admin/structure/views. The settings change only for the name of the View (in View name,  I used Menu_gerarchico) and the Display format in Create a Block (I selected Grid).

In Advanced you have to add inside RELATIONSHIPS Taxonomy term: Parent term.

Then in FIELDS (Parent) Taxonomy term: Name and Taxonomy term: Name

(Parent) Taxonomy term: NameTaxonomy term: Name

In FILTER CRITERIA (Parent) Taxonomy vocabulary: Machine name (= Tag)

In SORT CRITERIA (Parent) Taxonomy term: Name (asc) and Taxonomy term: Name (asc)

(Parent) Taxonomy term: Name (asc)Taxonomy term: Name (asc)

Finally on FORMAT: Grid | Settings, you have to click in the last link and change it in this way:

PROBLEM: in the preview section (at the bottom of the page), it is showed the resulting menu with all the tags, without any language filter. So I followed the solution in this discussion, and I added a custom module called: Taxonomy Views Translator.

Module to correct the language filter of terms

The custom module contains only two simple text files (taxonomy_views_translator.info and taxonomy_views_translator.module), I put them in taxonomy_views_translator folder that I copied in ftp:mio_sito/sites/all/modules/ and finally I activated from here http://my_site/admin/modules (in OTHERS section). The Module developer's guide can help you a lot!

taxonomy_views_translator.info:

name = Taxonomy Views Translator 
description = Provides taxonomy automatically translated in views
core = 7.x
files[] = taxonomy_views_translator.module

taxonomy_views_translator.module:

<?php 
function taxonomy_views_translator_views_query_alter(&$view, &$query) {
if ($view->name == 'menu_gerarchico') {
$query->where[] = array(
'conditions' => array(array(
'field' => 'taxonomy_term_data.language',
'value' => array('***CURRENT_LANGUAGE***','und'),
'operator' => 'in',
)),
'args' => array(),
'type' => 'AND',
);
}
}
?>

I only modified the following string:

'value' => array('***CURRENT_LANGUAGE***'),

In:

'value' => array('***CURRENT_LANGUAGE***','und'),

In order to have both localized and language neutral tags (i.e. PDF, drupal, etc.).

 


I put below two useful pictures to show you my settings, and the zip file: menu_relationships.zip, that contains my export view code called export_menu_relationships.txt (you can import in your Drupal site, it is sufficient to click in the import button in the Views menu page and to Paste view code here, but check the incompatibilities!), and my custom module for Drupal 7.


Left side
Right side



Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.