You are here

function custom_breadcrumbs_taxonomy_node_get_lightest_term in Custom Breadcrumbs 7.2

Same name and namespace in other branches
  1. 6.2 custom_breadcrumbs_taxonomy/custom_breadcrumbs_taxonomy.inc \custom_breadcrumbs_taxonomy_node_get_lightest_term()

Returns the lightest term for a given node.

If the term has parents, then the lightest parent's weight is used for the term weight. And if the parent has multiple child terms at different depths, the deepest child term will be returned. If the child terms have the same depth, then the lightest child term is returned.

Parameters

object $node: The node object.

Return value

object The taxonomy term object.

1 call to custom_breadcrumbs_taxonomy_node_get_lightest_term()
custom_breadcrumbs_taxonomy_node_get_term in custom_breadcrumbs_taxonomy/custom_breadcrumbs_taxonomy.inc
Returns the previous selected term or the lightest term for a given node.

File

custom_breadcrumbs_taxonomy/custom_breadcrumbs_taxonomy.inc, line 191
Helper functions for custom_breadcrumbs_taxonomy.

Code

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

        // Only consider terms in the lightest vocabulary.
        // @codingStandardsIgnoreLine
        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;
        }

        // @codingStandardsIgnoreLine
        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);
    }
  }
}