You are here

function _find_term in Location Taxonomize 7.2

Same name and namespace in other branches
  1. 7 location_taxonomize.module \_find_term()

Given a name, hierarchy level, and parent id, this function returns the term if such a term exists, and FALSE if it doesn't. It returns TRUE if the term was not found but should not be saved NOTE: $parentid is only necessary if $hlevel > 0. Otherwise, set $parentid = -1

1 call to _find_term()
location_taxonomize_process_item in ./location_taxonomize.module
Determines if a location needs to be taxonomized. If so, it saves the appropriate terms Returns an array containing two items: the first is the number of new terms saved, and the second is an array of all the terms that correspond to this object,…

File

./location_taxonomize.module, line 237

Code

function _find_term($name, $hlevel, $parentid) {
  $terms = taxonomy_get_term_by_name($name, 'location_taxonomize');

  // there are no terms that match
  if (!$terms) {
    return FALSE;
  }
  $withparent = array();

  // look through matching terms and find the one(s) with the right parent
  foreach ($terms as $term) {
    $parents = taxonomy_get_parents($term->tid);

    // for root-level terms:
    if (empty($parents) && $parentid == -1) {
      $withparent[] = $term;
    }
    elseif (array_pop($parents)->tid == $parentid) {
      $withparent[] = $term;
    }
  }

  // there is one term that matches exactly
  if (count($withparent) == 1) {
    return array_pop($withparent);
  }
  elseif (count($withparent) == 0) {
    return FALSE;
  }

  // if we get here, there was more than one term that matched exactly
  if (variable_get('location_taxonomize_show_messages', 1)) {
    $msg = t("Location taxonomize couldn't save a term because its hierarchy\n          location was ambiguous.");
    drupal_set_message($msg);
  }
  return TRUE;
}