Skip to content

Instantly share code, notes, and snippets.

@DuaelFr
Created February 10, 2016 10:58
Show Gist options
  • Save DuaelFr/8ba64645aa7e3154c0a6 to your computer and use it in GitHub Desktop.
Save DuaelFr/8ba64645aa7e3154c0a6 to your computer and use it in GitHub Desktop.
D8 Taxonomy tree to nested HTML list
<?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