You are here

public static function TaxonomyManagerTree::getFirstPath in Taxonomy Manager 8

Same name and namespace in other branches
  1. 2.0.x src/Element/TaxonomyManagerTree.php \Drupal\taxonomy_manager\Element\TaxonomyManagerTree::getFirstPath()

Helper function to calculate path.

Helper function that calculates the path to a child term and injects it into the json list structure.

1 call to TaxonomyManagerTree::getFirstPath()
TaxonomyManagerTree::processTree in src/Element/TaxonomyManagerTree.php

File

src/Element/TaxonomyManagerTree.php, line 197

Class

TaxonomyManagerTree
Taxonomy Manager Tree Form Element.

Namespace

Drupal\taxonomy_manager\Element

Code

public static function getFirstPath($tid, &$list) {
  $path = [];
  $next_tid = $tid;
  $i = 0;

  // Prevent infinite loop if inconsistent hierarchy.
  while ($i < 100) {
    $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;
    }
  }
}