Please see the comment submitted to Olav having to do with taxonomy_batch_operations.

I have included the following code snippet in order to clarify the issue, and not to suggest an actual solution.
But, the taxonomy system is such a good idea, that it may need a little more coercing into a solid API.
In fact, it might be nice if it could turn into some sort of extension to PHP.

At any rate, I wanted to get to the terms array in graceful way. And, I was unsure of the PHP exposition of static variables.
So, this snippet has a not so graceful way of exposing the terms array:

////
global $g_children, $g_parents, $g_terms;
$g_children = array();
$g_parents = array();
$g_terms = array();


function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
  global $g_children, $g_parents, $g_terms;

  $depth++;

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($g_children[$vid])) {
    $g_children[$vid] = array();

    $result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN  {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
    while ($term = db_fetch_object($result)) {
      $g_children[$vid][$term->parent][] = $term->tid;
      $g_parents[$vid][$term->tid][] = $term->parent;
      $g_terms[$vid][$term->tid] = $term;
    }
  }

  $max_depth = (is_null($max_depth)) ? count($g_children[$vid]) : $max_depth;
  if ($g_children[$vid][$parent]) {
    foreach ($g_children[$vid][$parent] as $child) {
      if ($max_depth > $depth) {
        $term = drupal_clone($g_terms[$vid][$child]);
        $term->depth = $depth;
        // The "parent" attribute is not useful, as it would show one parent only.
        unset($term->parent);
        $term->parents = $g_parents[$vid][$child];
        $tree[] = $term;

        if ($g_children[$vid][$child]) {
          $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
        }
      }
    }
  }

  return $tree ? $tree : array();
}

Comments

dpearcefl’s picture

Is there any interest still in the issue?

Anonymous’s picture

Status: Active » Closed (won't fix)