I have multiple level of vocabulary, I want to display all level in the hierarchy same as taxonomy display. My taxonomy is like.
All
Drinks
Alcohol Drinks
Beer
Vodka
Alcohol Free Drinks
Coca Cola
Pepsi
Fruits
Fruits For Losing Weight
Watermelon
Blackberrie
Fruits For Dogs
Strawberry
Orange
now I am trying to load this using code.
$vid = 'food';
$terms =Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1, FALSE);
foreach ($terms as $term) {
$term_data_1[$term->tid] = $term->name;
$terms_2 =Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term->tid, 1, FALSE);
foreach ($terms_2 as $term_2) {
$term_data_2[$term_2->tid] = $term_2->name;
$terms_3 =Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term_2->tid, 1, FALSE);
foreach ($terms_3 as $term_3) {
$term_data_3[$term_3->tid] = $term_3->name;
}
}
$term_data_1[$term_2->tid] = $term_data_2;
$term_data_1[$term_3->tid] = $term_data_3;
}
kint($term_data_1);
and this code giving result like
Need help to get term in hierarchy.