You are here

function content_taxonomy_node_import_get_children in Node import 6

Used to contruct the lineage of terms for use by hierarchical select widget.

Parameters

<type> $lineage:

<type> $vid:

<type> $parent_tid:

Return value

<type>

1 call to content_taxonomy_node_import_get_children()
content_taxonomy_node_import_values_alter in supported/content_taxonomy/content_taxonomy.inc
Implementation of hook_node_import_values_alter()

File

supported/content_taxonomy/content_taxonomy.inc, line 203
Support file for the CCK Content Taxonomy module

Code

function content_taxonomy_node_import_get_children($lineage, $vid, $parent_tid = NULL) {
  $top = array_shift($lineage);
  $remainder = $lineage;
  if (is_numeric($top)) {
    $foundterms = taxonomy_get_term(intval($top));
  }
  else {
    $top = content_taxonomy_node_import_check_numeric($top);
    $foundterms = taxonomy_get_term_by_name($top);
  }

  //first, let's clear out any terms in other vocabularies
  foreach ($foundterms as $index => $termfound) {
    if ($termfound->vid != $vid) {
      unset($foundterms[$index]);
    }
  }
  if (empty($foundterms)) {
    node_import_input_error(t("The term %value was not found at the expected level in your taxonomy hierarchy. Please check your import file and try again.", array(
      '%value' => $top,
    )));
    return NULL;
  }

  //let's take what we have left and make sure we found the right child - there should just be one term left after the parent sifting that follows
  $current_tid = 0;
  foreach ($foundterms as $index => $termfound) {
    $tid = $termfound->tid;
    $parents = taxonomy_get_parents($tid);
    if ($parent_tid != NULL && !isset($parents[$parent_tid]) || $parent_tid == NULL && !empty($parents)) {
      unset($foundterms[$index]);

      //we found a term whose text matched, but isn't in the right spot of the hierarchy
      continue;
    }
    else {
      $current_tid = $tid;
    }

    //drupal_set_message('<pre>Parents: ' . print_r($parents, TRUE) . '</pre>');
    $terms[$tid] = (array) $termfound;
    $children = taxonomy_get_children($tid, $vid);
    $terms[$tid]['children'] = $children;
    $terms[$tid]['parents'] = $parents;
  }
  if (empty($terms)) {
    node_import_input_error(t("The term %value was not found at the expected level in your taxonomy hierarchy, but a match was found at a different level. Please check your import file and try again.", array(
      '%value' => $top,
    )));
    return NULL;
  }
  if (count($terms) > 1) {
    node_import_input_error(t("The term %value was found MORE THAN ONCE as children of the same term. Please check your import file and try again.", array(
      '%value' => $top,
    )));
    return NULL;
  }
  if (is_array($remainder) && !empty($remainder)) {

    //drupal_set_message('<pre>Terms inside: ' . print_r($terms, TRUE) . '</pre>');
    $child_terms = content_taxonomy_node_import_get_children($remainder, $vid, $current_tid);
    if ($child_terms != NULL) {
      $terms = array_merge($terms, $child_terms);
    }
  }
  return $terms;
}