You are here

function xmlsitemap_taxonomy_get_term_depth in XML sitemap 7.2

Same name and namespace in other branches
  1. 6.2 xmlsitemap_taxonomy/xmlsitemap_taxonomy.module \xmlsitemap_taxonomy_get_term_depth()

Find the tree depth of a taxonomy term.

Parameters

object $term: A taxonomy term object.

Return value

array The tree depth of the term.

File

xmlsitemap_taxonomy/xmlsitemap_taxonomy.module, line 209
Main file for XML sitemap taxonomy.

Code

function xmlsitemap_taxonomy_get_term_depth(stdClass $term) {
  static $depths = array();
  if (!isset($depths[$term->tid])) {
    if ($parent = db_query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = %d", $term->tid)
      ->fetchField()) {

      // If the term has a parent, the term's depth is the parent's depth + 1.
      if (!isset($depths[$parent])) {
        $depths[$parent] = xmlsitemap_taxonomy_get_term_depth($parent);
      }
      $depths[$term->tid] = $depths[$parent] + 1;
    }
    else {

      // Term has no parents, so depth is 0.
      $depths[$term->tid] = 0;
    }
  }
  return $depths[$term->tid];
}