Taxonomy navigation block module?

xweb - January 14, 2007 - 03:41

I can't seem to find a module for drupal 5 which will give me a expandable block and page list of a taxonomy and it's child terms which link to a node list. I'm, sure there is a simple solution for this as I'd assume this is a commonly required feature ( i have needed it more than once)

Previously I've used taxonomy_dhtml module or snippets with my own crude hacks although this works it makes upgrades near impossible.

I've tried views module and category module but these seem to complicate a seemingly simple feature.

Any ideas?

Bump

Zoologico - January 14, 2007 - 18:53

Tracking.

Zoologico, are you looking for..

xweb - January 15, 2007 - 05:06

this feature or did you bump for a duplicate post of yours?

Anyway, I've written a block snippet for 5.0 which will take the top level terms of a vocabulary and make them collapsable(.js) so that child terms are hidden by default. If anyone is interested theres a demo on my site ATM for a better idea of what i'm doing. http://www.xweb.com.au

The structure of the menu is:

Test Menu(vocab)
-->Design (a top level/parent term and submenu title)
*Domains (a child term to Design term)
*Hosting (ditto)
*domain sub term (a child of the Domains child term should be indented)

This makes sense if your see the demo on my site.

Hope this helps someone.
Cheers, Chris.
xweb

xweb. Could really do with

lucidmoon - February 23, 2007 - 10:13

xweb.

Could really do with this functionality, any chance of the source?

M

This is my block snippet

xweb - February 26, 2007 - 09:58

not pretty but works :P
Maybe we can make this a module if there is enough interest.

<?php

// accordion taxonomy menu
// by Chris Herberte
// ** don't forget to change $vocabulary --vv
//

$vocabulary = 4;

drupal_add_js('misc/collapse.js');
$lastdepth = 1;
$topoflist = 1;
$nid = 0;

if (
arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
 
$nid = arg(1);
 
$terms = taxonomy_node_get_terms($nid);
 
rsort($terms);
 
$tid = $terms[0]->tid;
}
elseif (
arg(0) == 'taxonomy' && arg(1) == 'term') {
 
$terms = preg_split('/[+ ,]/', arg(2));
 
$tid = $terms[0];
}

$parents = taxonomy_get_parents($tid);
foreach (
$parents as $parent) {
$i++;
}

$tree = taxonomy_get_tree($vocabulary);

foreach (
$tree as $term) {

  if (
$lastdepth == 1 && $term->depth == 0 ) {
   
$legend = $term->name;
    if (
$topoflist == 0) {
      print
"</ul></div></fieldset>";
    }

    if (
$term->tid == $parent->tid) {
       print
"<fieldset class='collapsible'><legend>$legend</legend><div class='description'><ul>";
    }
    else {
       print
"<fieldset class='collapsible collapsed'><legend>$legend</legend><div class='description'><ul class='menu'>";
    }
   
$topoflist = 0;
  }

  if (
$term->depth > 0) {

    if (
$term->tid == $tid) {
       print
"<li class='collapsed' id='darker'>" . l($term->name, "taxonomy/term/$term->tid") . "</li>\n";
    }
    else {
       print
"<li class='leaf notdarker'>" . l($term->name, "taxonomy/term/$term->tid") . "</li>\n";
    }
  }

 
$lastdepth = $term->depth;
}
print
"</ul></div></fieldset>";
?>

xweb

Problem with displaying all parent terms

lejon - August 28, 2008 - 14:06

Hi,

This works great apart from one thing. I have parent terms which work as containers for sub-terms. Some of my parent terms are not showing up as the nodes are tagged only with their sub-terms.

Is there a way of showing all parent terms, even if no nodes are associated with them?

Hi,Great snippet, but error

Summit - September 11, 2008 - 22:29

Hi,

It is not working correct, only the first three of my head terms are showing.
And all subterms of the not showing terms are shown in the head terms.
My Head terms are:
-Amerika
-Africa
-Asia
-Europa
-New Sealand
-Australia

The menu gives
>Amerika
>Africa
-Asia
and in Asia

>Asia
----Holland
----Switserland
etc..

This is off course not correct...
Thanks for going into this!
greetings,
Martijn

greetings,
Martijn

Revised and Drupalized (D5)

Chris Herberte - October 10, 2008 - 00:31

<?php
$vid
= 1;

$tree = taxonomy_get_tree($vid, 0, -1, 1);

foreach (
$tree as $term) {
 
$items = array();
 
$children = taxonomy_get_children($term->tid, $vid, 'tid');
  foreach (
$children as $child) {
   
$items[] = l($child->name, 'taxonomy/term/'. $child->tid);
  }
 
$fieldset = array(
   
'#title' => $term->name,
   
'#collapsible' => TRUE,
   
'#collapsed' => TRUE,
   
'#value' => theme('item_list', $items),
   
'#attributes' => array('class' => 'accordion'),
  );
  print
theme('fieldset', $fieldset);
}
?>

http://www.xweb.com.au

this is close, but not working completely

lejon - October 15, 2008 - 08:24

Hi,

Thanks for this - it works much better BUT for those nodes which are tagged only with a parent term there is no way of accessing them. When you click on a parent term which has no children you get the uncollpased (and empty) list rather than a list of nodes tagged with those parent terms.

Is there an easy way of fixing this?

I would like all parent terms to be links as well as collapsible lists.

Thanks!

possible for varying levels?

dreeds - January 26, 2009 - 07:25

This is exactly what I'm looking for with one exception - it only displays to two levels deep. I have a taxonomy with this structure:

- term1
--child1 of term1
-term2
--child1 of term2
--child2 of term2
---child of child2 of term2
etc

Does anyone know how to modify the above code so it makes any term no matter at what level it is collapsible if it has child terms?

 
 

Drupal is a registered trademark of Dries Buytaert.