Here is my initial pass at creating a nested and collapsible list of taxonomy terms with unlimited levels. This code makes the deepest term in the nesting a link to the term page.
i started with a node template file ('node-page.tpl.php') so i can wrap it and then make a menu item to it. Make a page type node and give it a title that designates a root level taxonomy page. Inside 'node-page.tpl.php' put the following code: (please note that it would be best to put the code inside the case into template.php and pass it as var)
<?php
// $title of course is just the value entered in the title field for the designated node.
switch ($title) {
case 'My Taxonomy Categories':
// $vid = the ID of taxonomy vocabulary to create collapsible hierarchical lists
$vid = 1;
$tree = taxonomy_get_tree($vid);
if ($tree) {
drupal_add_js('misc/collapse.js');
$total_terms = count($tree);
foreach ($tree as $key => $term) {
$depth = $term->depth;
if ($key == 0) { // first term
if ($total_terms == 1) {
print ('<ul><li>' . l($tree[$key]->name, 'taxonomy/term/' . $tree[$key]->tid) . '</li></ul>');
}
} else { // all terms except first
if ($depth > $last_depth) { // previous term is a parent
print ('<fieldset class="collapsible collapsed"><legend><a href="#">' . $tree[$key-1]->name . '</a></legend>');
if ($depth >= $tree[$key+1]->depth) {
print ('<ul>');
}
} else if ($depth == $last_depth) { // previous term is either same level linked term or same level parent
if ($depth >= $tree[$key+1]->depth) { // same level link term
print ('<li>' . l($tree[$key-1]->name, 'taxonomy/term/' . $tree[$key-1]->tid) . '</li>');
} else { // same level parent
if ($key > 1) { // previous parent is not the first so close fieldset
print ('</fieldset>');
}
print ('<fieldset class="collapsible collapsed"><legend><a href="#">' . $tree[$key-1]->name . '</a></legend>');
}
} else { // $depth < $last_depth - ie previous term is deeper
print ('<li>' . l($tree[$key-1]->name, 'taxonomy/term/' . $tree[$key-1]->tid) . '</li></ul>');
$difference_in_depth = $last_depth-$depth;
for ($i = 1; $i <= $difference_in_depth; $i++) {
print ('</fieldset>');
}
}
if ($key == ($total_terms-1)) { // last term
print ('<li>' . l($tree[$key]->name, 'taxonomy/term/' . $tree[$key]->tid) . '</li></ul>');
$difference_in_depth = $last_depth-$depth;
for ($i = 1; $i <= $difference_in_depth; $i++) {
print ('</fieldset>');
}
}
} // end ifs ('depth' compared to 'last_depth')
$last_depth = $depth;
} // end foreach
} // end if ($tree)
break;
}
?>
This code could probably use a little logic and comments cleaning but it seems to work for me and provides a relatively decent structure to extend logic or theming.
[EDIT - doesn't work in all cases of nesting multiple categories - works for two level deep - need logic work]
Comments
A nice Drupal way to handle it
OK the code above is a mess. Furthermore it is improper html to place fieldsets inside lists.
Since i posted the above code over a year ago, i've delved deeply into recursive functions and came up with an elegant solution.
In your template file insert the following:
In your custom module insert the following (exclude opening & closing php tag):
Associated nodes?
Wow, this is great! Is there a simple way to display an array of nodes associated with each tid? Or is this way out of scope?
I am getting an
I am getting an error.
Undefined variable: nested_tree in taxonomy_get_nested_tree() (line 18 of C:\wamp\www\drupal-7.2\sites\all\modules\taxanomyterm\taxanomyterm.module).
Can you please tell me what is this about and where i need to paste the code.I have created nmy own module on which i have written your code in .module file and written the template code in the template.php file.
What else i need to do on this.Please let me know
I was searching for a long
I was searching for a long time to make the same thing with the display of the nodes in each terms until I found the Taxonomy Lineage module. I'm trying to make the list collapsible the way you did now. Great work.
Drupal Newbie Files fix
Okay, this is an answer to what i have been looking for. Please, for thos of us new to drupal or not the code lords, where do we create these files to....? THanks.