I have multiple level of vocabulary, I want to display all level in the hierarchy same as taxonomy display. My taxonomy is like.

      Alcohol Drinks
      Alcohol Free Drinks
         Coca Cola
      Fruits For Losing Weight
      Fruits For Dogs

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;


and this code giving result like

enter image description here

Need help to get term in hierarchy.


TermForm.php has an example of how to do this.

Basically you loop through the tree and add dashes according to the depth of the term.

Here's an example you can use:

$options = [];
foreach (\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid) as $item) {
  $options[$item->tid] = str_repeat('-', $item->depth) . $item->name;

UPDATE: If you want a nested array of terms, you can do like this:

$result = [];
foreach (\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid) as $item) {
  $parents = array_reverse(\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadAllParents($item->tid), TRUE);
  $r = &$result;
  foreach ($parents as $k => $v) {
    if (isset($r[$k])) {
      $r[$k] = array_replace($r[$k], [$k => $v->label()]);
    else {
      $r[$k] = [$k => $v->label()];
    $r = &$r[$k];

That will work regardless of how many levels your terms are nested.

|improve this answer|||||
  • I got that actually I dont want to make (-) I want hierarchy taxonomy array in the dpm, thats why I post image also, there inner level are not coming in correct hierarchy level. – Mukunda Bhatta Nov 26 '19 at 16:30

You should use Multi Dimensional array for storing data.

  $vid = 'tags';
  $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
  $termDataAll = [];
  foreach ($terms as $term) {
    $termDataAll[$term->tid]["tid"] = $term->tid;
    $termDataAll[$term->tid]["name"] = $term->name;
     $terms_2 =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term->tid, 1);
      foreach($terms_2 as $term_2){
        $termDataAll[$term->tid][$term_2->tid]["tid"] = $term_2->tid;
        $termDataAll[$term->tid][$term_2->tid]["name"] = $term_2->name;
        $terms_3 =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term_2->tid, 1);
        foreach($terms_3 as $term_3){
          $termDataAll[$term->tid][$term_2->tid][$term_3->tid]["tid"] = $term_3->tid;
          $termDataAll[$term->tid][$term_2->tid][$term_3->tid]["name"] = $term_3->name;
|improve this answer|||||
  • This would not work if the array tree is nested 4 levels. – user72672 Nov 27 '19 at 6:55
  • The code can be modified for any number of levels by adding required number of for loops similarly. – Sandesh Yadav Nov 27 '19 at 7:06
  • 1
    I know. This code might work, but it's not really nice to have some code that won’t work if they add mare levels of terms. It would be better to handle all possible solutions with one chunk of code. – user72672 Nov 27 '19 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.