/**
  * Helper function that calculates the path to a child term and injects it
  * into the json list structure.
  */
 public static function getFirstPath($tid, &$list)
 {
     $path = array();
     $next_tid = $tid;
     $i = 0;
     while ($i < 100) {
         //prevent infinite loop if inconsistent hierarchy
         $parents = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($next_tid);
         if (count($parents)) {
             // Takes first parent.
             $parent = array_shift($parents);
             $path[] = $parent;
             $next_tid = $parent->id();
             if (TaxonomyManagerTree::isRoot($parent->id())) {
                 break;
             }
         } else {
             break;
         }
         $i++;
     }
     if (count($path)) {
         $path = array_reverse($path);
         $root_term = $path[0];
         foreach ($list as $current_index => $list_item) {
             if ($list_item['key'] == $root_term->id()) {
                 $index = $current_index;
                 break;
             }
         }
         if (isset($index)) {
             $path[] = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);
             $list[$index]['children'] = TaxonomyManagerTree::getPartialTree($path);
             $list[$index]['lazy'] = FALSE;
             $list[$index]['expanded'] = TRUE;
         }
     }
 }