You are here

function taxonomy_csv_term_get_first_path in Taxonomy CSV import/export 6.5

Same name and namespace in other branches
  1. 7.5 taxonomy_csv.term.api.inc \taxonomy_csv_term_get_first_path()

Return the first path to the root of a term.

Parameters

$term: A term object with parents attribute, as obtained with taxonomy_get_tree().

$tree: A tree array as obtained with taxonomy_get_tree().

Return value

Array of term objects matching to the path of a term to its root term. If a term is a root term, return an empty array.

1 call to taxonomy_csv_term_get_first_path()
taxonomy_csv_term_export in export/taxonomy_csv.export.api.inc
Export a term to a line matching the options.

File

./taxonomy_csv.term.api.inc, line 309
Find, get and set full or detail term items.

Code

function taxonomy_csv_term_get_first_path($term, &$tree) {
  $path = array();

  // To use a counter prevents infinite loop when the hierarchy is inconsistent.
  $i = 0;
  while ($i < 100 && isset($term->parents) && !empty($term->parents) && $term->parents[0] != 0) {
    $tid = $term->parents[0];
    if ($tid > 0) {

      // Get the full term from the tree.
      foreach ($tree as $parent) {
        if ($parent->tid == $tid) {
          break;
        }
      }
      $path[] = $term = $parent;
    }
    else {
      break;
    }
    $i++;
  }

  // The path is reversed in order to begin with root term.
  return array_reverse($path);
}