You are here

function taxonomy_csv_term_get_first_path in Taxonomy CSV import/export 7.5

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

Return the first path to the root of a term.

@note Drupal and taxonomy_csv use 'parent' property, but taxonomy_get_tree() uses 'parents'.

Parameters

$term: A term object with 'parent' property.

$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 139
Find, get and set full or detail term items.

Code

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

  // Items need to be ordered from 0 to get first parent easy.
  if (isset($term->parent)) {
    $term->parent = array_values($term->parent);
  }
  elseif (isset($term->parents)) {
    $term->parent = $term->parents;
    unset($term->parents);
  }

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

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

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