You are here

function crumbs_EntityPlugin_Field_TermReference::fieldFindCandidate in Crumbs, the Breadcrumbs suite 7.2

Parameters

array $items:

Return value

string

Overrides crumbs_EntityPlugin_Field_Abstract::fieldFindCandidate

File

lib/EntityPlugin/Field/TermReference.php, line 8

Class

crumbs_EntityPlugin_Field_TermReference

Code

function fieldFindCandidate(array $items) {
  $terms = array();
  foreach ($items as $item) {
    $terms[$item['tid']] = TRUE;
  }
  if (count($terms) > 1) {
    $walk = $terms;
    $visited = array();
    while (!empty($walk)) {
      $visited += $walk;
      foreach ($walk as $tid => $true) {
        $parents = taxonomy_get_parents($tid);
        unset($walk[$tid]);
        foreach ($parents as $parent_tid => $parent) {
          unset($terms[$parent_tid]);
          if (!isset($visited[$parent_tid])) {
            $walk[$parent_tid] = $parent;
          }
        }
      }
    }
  }

  // Return the path of the first found term, if any.
  foreach ($terms as $tid => $term_info) {
    $term = taxonomy_term_load($tid);
    if (!empty($term)) {
      $uri = entity_uri('taxonomy_term', $term);
      if (!empty($uri)) {
        return $uri['path'];
      }
    }
  }
  return NULL;
}