You are here

function _taxonomy_breadcrumb_node_get_lightest_term in Taxonomy Breadcrumb 6

Same name and namespace in other branches
  1. 7 taxonomy_breadcrumb.inc \_taxonomy_breadcrumb_node_get_lightest_term()

Return lightest term for a given node.

@ return The lightest term object associated with the node.

Parameters

$node: The node object.

1 call to _taxonomy_breadcrumb_node_get_lightest_term()
taxonomy_breadcrumb_nodeapi in ./taxonomy_breadcrumb.module
Implementation of hook_nodeapi().

File

./taxonomy_breadcrumb.inc, line 47
helper functions for taxonomy_breadcrumb

Code

function _taxonomy_breadcrumb_node_get_lightest_term($node) {
  $terms = taxonomy_node_get_terms($node);
  if (!empty($terms)) {
    if (count($terms) > 1) {
      foreach ($terms as $term) {

        // Only consider terms in the lightest vocabulary.
        if (!isset($vid)) {
          $vid = $term->vid;
        }
        elseif ($term->vid != $vid) {
          continue;
        }

        // If the term has parents, the weight of the term is the weight of the lightest parent.
        $parents = taxonomy_get_parents_all($term->tid);
        $depth = count($parents);
        if ($depth > 0) {
          $parent = array_pop($parents);
          $weight = $parent->weight;
        }
        else {
          $weight = $term->weight;
        }
        if (isset($lweight) && $weight < $lweight || !isset($lweight)) {
          $lterm = $term;
          $lweight = $weight;
          $ldepth = $depth;
        }
        elseif (isset($lweight) && $weight == $lweight) {

          // If the node has multiple child terms with the same parent, choose the child with the greatest depth.
          if ($depth > $ldepth) {
            $lterm = $term;
            $ldepth = $depth;
          }
          elseif ($depth == $ldepth) {

            // If the terms have the same depth, pick the term with the lightest weight.
            $lterm = $lterm->weight < $term->weight ? $lterm : $term;
          }
        }
      }
      return $lterm;
    }
    else {
      return array_pop($terms);
    }
  }
}