By sylvaticus on
All I want is display ALL the taxonomy terms of a given vocabolary in a hierarchical tree with their descriptions.
I managed to get the nested array from http://api.drupal.org/api/function/taxonomy_get_tree
But how do I now traverse it to get (and print) the terms in a html list?
Comments
he-he-he.. internet is
he-he-he.. internet is small.. today I was looking for the same thing... and google showed me.. my own old request!
I think I understood http://api.drupal.org/api/function/taxonomy_get_tree doesn't even return a nested array.. so I am at the same point: How do I show (echo) a nested vocaboulary on a page using PHP filter?
Thanks to
Thanks to http://api.drupal.org/api/drupal/modules--taxonomy--taxonomy.module/func... I managed to get this code:
However I display only the first level. I think that's because I'm not placing the funcion on a .tpl but simply on a page. So the last question is how can I recursivelly call theme_taxonomy_nested_tree() ??
Finally I got it
Finally I got it :-))))))
Of course it can be easily changed to point each term to a view that take the $tid as argument...
This is indeed a nice way to
This is indeed a nice way to list the terms.
I'm going to start with your functions to get my taxonomy terms in a select dropdown.
With some brainbreaking and hopefully not to many problems with ajax it's gonna turn out really nice.
Thnx for this nice script!
Jurgen.
Drupal backend developer since 2011.
Open Source Webdevelopment Coding Blog
But
But where to put your code. As I paste your code in the body field of a page with php filter but it did not work.
Ahmad Nawaz
Acquia Certified Developer
Email: hmdnawaz@gmail.com
Skype: hmdnawaz
Ahmad, In case you still
Ahmad,
In case you still want to know how..see the 2, 10 in the brackets? 2 is the vocabulary id, and i think 10 is the depth..so for me I used 1, 2 and got the beautifully indented listing of my taxonomy terms with only one sub level.
Jaya
Drupal 7
Updated for Drupal 7:
Your code worked like charm.
Your code worked like charm. Thanks a lot.
Very good!
Thank you @jhyrith it is very useful :)
Thanks!
@jhyrith code works like a charm!
Update for Drupal 8
Update for drupal 8.
I simply had to change the line
For
You'll have to replace "vocabulary_id" with your concerned vocabulary_id.
Works well for a single term but if 2 terms id are specified in parameter of the taxonomy_get_nested_tree() function only the first term id is covered (it does not matter to me I do not need to travel more than one term).
And adapted version to return an array instead of an html list. Compatible (for example) with the script bootstrap-treeview.js (https://github.com/jonmiles/bootstrap-treeview).
Idem, works well for a single term but if 2 terms id are specified in parameter of the taxonomy_get_nested_tree() function only the first term id is covered.
thanks for your code but the
Thanks for your code but the menu elements are being print in a flat manner like the image attached(Flat Mannered) but they should actually print the hierarchical manner like the image attached(Hierarchy ) and should work as link references but they arent There is small correction and i made corrections to your code.
Updated Code for Drupal 7
I've made some updates after
I've made some updates after some PHP warnings and the way to pass the $vid into the loadTree since it was failing in Drupal 8.5.3 with php 7.1, I hope it helps someone.
function taxonomy_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {if (is_int($terms)) {$vid = 'YOUR_TAXONOMY_VOCABULARY';
$terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
}
$return = array();
foreach($terms as $term) {
foreach($term->parents as $term_parent) {
if ($term_parent == $parent) {
$return[$term->tid] = $term;
}
else {
$parents_index[$term_parent][$term->tid] = $term;
}
}
}
foreach($return as &$term) {if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
$term->children = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
}
}
return $return;}
function output_taxonomy_nested_tree($tree) {$output = '';
if (count($tree)) {$output = '<ul class="taxonomy-tree">';
foreach ($tree as $term) {
$output .= '<li class="taxonomy-term">';
$output .= $term->name;
if (isset($term->children)) {
$output .= output_taxonomy_nested_tree($term->children);
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;}
$tree= taxonomy_get_nested_tree(2);$output=output_taxonomy_nested_tree($tree);
return $output;
loadTree() already returns
loadTree() already returns the whole tree, as a flat list with a depth argument. that can be used directly to show it, with a single foreach loop that opens and loses <ul> tags depending on depth changes. Additionally, loading terms as entities allows to make the link generation much, much simpler as well:
The code above will render an
The code above will render an incorrect nested html list. The sub list should be included into the list item element.
I agree that it is better to call loadTree() only once as it returns the full tree. It is also preferred to use a twig template for the rendering part of the taxonomy tree. I created a small wrapper class for loadTree with a simple template. The switch block could probably be improved/removed. In my case only a depth of tree is needed to it works for now.