You are here

function _taxonomy_manager_tree_get_first_path in Taxonomy Manager 6.2

Same name and namespace in other branches
  1. 7 taxonomy_manager.module \_taxonomy_manager_tree_get_first_path()

calculates a path to a certain term and merges it into the tree

1 call to _taxonomy_manager_tree_get_first_path()
taxonomy_manager_tree_process_elements in ./taxonomy_manager.module
Processes the tree form element

File

./taxonomy_manager.module, line 517
Taxonomy Manager

Code

function _taxonomy_manager_tree_get_first_path($tid, &$tree, &$terms_to_expand) {
  $path = array();
  $next_tid = $tid;
  $i = 0;
  while ($i < 100) {

    //prevent infinite loop if inconsistent hierarchy
    $parents = taxonomy_get_parents($next_tid);
    if (count($parents)) {

      //takes first parent
      $parent = array_pop($parents);
      $path[] = $parent;
      $next_tid = $parent->tid;
      if (taxonomy_manager_term_is_root($next_tid)) {
        break;
      }
    }
    else {
      break;
    }
    $i++;
  }
  $path = array_reverse($path);
  $path[] = taxonomy_get_term($tid);
  $root_term = $path[0];
  $root_term_index;
  if (count($path) > 1) {
    foreach ($tree as $index => $term) {
      if ($term->tid == $root_term->tid) {
        $root_term_index = $index;
        break;
      }
    }
  }
  if (isset($root_term_index)) {
    $path_tree = taxonomy_manager_get_partial_tree($path);
    $first_part = array_slice($tree, 0, $root_term_index + 1);
    $last_part = array_slice($tree, $root_term_index + 1, count($tree));
    $tree = array_merge($first_part, $path_tree, $last_part);
  }
}