function synonyms_get_term_by_synonym in Synonyms 7
Try to find a term by its name or synonym.
You are advised to use the more general function synonyms_get_entity_by_synonym() unless you really need $parent input argument from this function for additional filtering by Taxonomy hierarchy.
Parameters
string $name: The string to be searched for its {taxonomy_term_data}.tid
object $vocabulary: Fully loaded vocabulary object in which you wish to search
int $parent: Optional. In case you want to narrow your search scope, this parameter takes in the {taxonomy_term_data}.tid of the parent term, letting you search only among its children
Return value
int If the look up was successful returns the {taxonomy_term_data}.tid of the found term, otherwise returns 0
2 calls to synonyms_get_term_by_synonym()
- SynonymsSynonymsWebTestCase::testSynonyms in ./
synonyms.test - Test the functionality of synonyms.
- synonyms_add_term_by_synonym in ./
synonyms.module - Look up a term considering synonyms and if nothing is found add one.
File
- ./
synonyms.module, line 594 - Provide synonyms feature for Drupal entities.
Code
function synonyms_get_term_by_synonym($name, $vocabulary, $parent = 0) {
if (!module_exists('taxonomy')) {
return 0;
}
$name = trim($name);
$terms = taxonomy_get_term_by_name($name, $vocabulary->machine_name);
foreach ($terms as $term) {
if (!$parent || synonyms_taxonomy_term_is_child_of($term->tid, $parent)) {
// TODO: actually it could be so that there is more than 1 term that
// satisfies the search query, i.e. the name and parent constraints. At
// the moment we are going to return the first one we encounter, though
// something better could be thought of in the future.
return $term->tid;
}
}
// We have failed to find a term with the provided $name. So let's search now
// among the term synonyms.
$bundle = field_extract_bundle('taxonomy_term', $vocabulary);
$synonyms = synonyms_synonyms_find(db_and()
->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $name), 'taxonomy_term', $bundle);
foreach ($synonyms as $synonym) {
if (!$parent || synonyms_taxonomy_term_is_child_of($synonym->entity_id, $parent)) {
// TODO: similarly here, as above, we could have more than 1 match, but
// for now we will simply return the first one encountered.
return $synonym->entity_id;
}
}
// If we have reached down here, this means we haven't got any match
// as fallback we return 0.
return 0;
}