PicaTeclas

20may/090

Mostrar un listado anidado de términos de taxonomía en Drupal

Buscando cómo mostrar un listado aninado de los términos (terms) de un vocabulario (una categoría de Drupal, o taxonomy), encontré este útil fragmento de código en Drupal.org. Para que funcione esto es necesario usar el módulo Taxonomy.

<?php
            // El ID del vocabulario de la taxonomia del que queremos crear la lista de terminos
            $vid = 10;
 
            $depth = 0;
            $num_at_depth = 0;
            $tree = taxonomy_get_tree($vid);
 
            print "<ul class=\"menu\">\n<li>";
            foreach ($tree as $term) {
            $diffdepth=0;
              if ($term->depth > $depth) {
                print "\n<ul>\n<li>";
                $depth = $term->depth;
                    $num_at_depth = 0;
              }
              if ($term->depth < $depth) {
                $diffdepth= $depth -$term->depth;
                while ($diffdepth > 0){
                    print "</li>\n</ul>\n";
                    $diffdepth -- ; 
                }
                    $depth = $term->depth;
              }
              if (($term->depth == $depth) && ($num_at_depth > 0)) {
                  print "</li>\n<li>";
                }
              print l($term->name, 'taxonomy/term/' . $term->tid);
                $num_at_depth ++;
            }
            print "</li>\n</ul>\n";
?>

El crédito va al autor, billyboylindien