Created
February 10, 2016 10:58
-
-
Save DuaelFr/8ba64645aa7e3154c0a6 to your computer and use it in GitHub Desktop.
D8 Taxonomy tree to nested HTML list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$vocabulary_name = 'my_machine_name'; | |
$root_tid = 0; | |
$vocabulary = $this->vocabularyStorage->load($vocabulary_name); | |
$terms = $this->termStorage->loadTree($vocabulary->id(), $root_tid); | |
$tree = [$root_tid => []]; | |
foreach ($terms as $term) { | |
$tree[$term->tid] = [ | |
'title' => ['#markup' => $term->name], | |
]; | |
if (empty($tree[$term->parents[0]]['children'])) { | |
$tree[$term->parents[0]]['children'] = [ | |
'#theme' => 'item_list', | |
'#items' => [], | |
]; | |
} | |
$tree[$term->parents[0]]['children']['#items'][] = &$tree[$term->tid]; | |
} | |
$build = $tree[$root_tid]['children']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment