| 7 taxonomy.module | taxonomy_get_term_by_name($name, |
| 4.6 taxonomy.module | taxonomy_get_term_by_name($name) |
| 4.7 taxonomy.module | taxonomy_get_term_by_name($name) |
| 5 taxonomy.module | taxonomy_get_term_by_name($name) |
| 6 taxonomy.module | taxonomy_get_term_by_name($name) |
Try to map a string to an existing term, as for glossary use.
Provides a case-insensitive and trimmed mapping, to maximize the likelihood of a successful match.
Parameters
$name: Name of the term to search for.
$vocabulary: (optional) Vocabulary machine name to limit the search. Defaults to NULL.
Return value
An array of matching term objects.
5 calls to taxonomy_get_term_by_name()
- TaxonomyHooksTestCase::testTaxonomyTermHooks in modules/
taxonomy/ taxonomy.test - Test that hooks are run correctly on creating, editing, viewing, and deleting a term.
- TaxonomyTermTestCase::testNodeTermCreationAndDeletion in modules/
taxonomy/ taxonomy.test - Test term creation with a free-tagging vocabulary from the node form.
- TaxonomyTermTestCase::testTaxonomyGetTermByName in modules/
taxonomy/ taxonomy.test - Test taxonomy_get_term_by_name().
- TaxonomyTermTestCase::testTermInterface in modules/
taxonomy/ taxonomy.test - Save, edit and delete a term using the user interface.
- TaxonomyTermTestCase::testTermMultipleParentsInterface in modules/
taxonomy/ taxonomy.test - Test saving a term with multiple parents through the UI.
File
- modules/
taxonomy/ taxonomy.module, line 1215 - Enables the organization of content into categories.
Code
function taxonomy_get_term_by_name($name, $vocabulary = NULL) {
$conditions = array('name' => trim($name));
if (isset($vocabulary)) {
$vocabularies = taxonomy_vocabulary_get_names();
if (isset($vocabularies[$vocabulary])) {
$conditions['vid'] = $vocabularies[$vocabulary]->vid;
}
else {
// Return an empty array when filtering by a non-existing vocabulary.
return array();
}
}
return taxonomy_term_load_multiple(array(), $conditions);
}
Comments
term by vocabulary?
PermalinkUnbelivable, that this function still has no parameter to filter the result by vid. Even though the they recommand to have the $options parameter deprecated for the inner function, you can use this for the vid filter.
function taxonomy_get_term_by_name_and_vocabulary($name, $vid) {return taxonomy_term_load_multiple(array(), array('name' => trim($name), 'vid' => $vid));
}
Should work.
Non-deprecated way to get tid from term name in a vocabulary
PermalinkIf you have the term name and want to find its term id in a particular vocabulary, this function could help. For e.g. since terms do not have machine names, they cannot be exported by features, unless their term ids match in the different dbs.
/*** Helper function to dynamically get the tid from the term_name
*
* @param $term_name Term name
* @param $vocabulary_name Name of the vocabulary to search the term in
*
* @return Term id of the found term or else FALSE
*/
function _get_term_from_name($term_name, $vocabulary_name) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) {
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $term) {
if ($term->name == $term_name) {
return $term->tid;
}
}
}
return FALSE;
}
Use EntityFieldQuery instead
PermalinkInstead of loading and looping over the entire taxonomy tree which could be potentially large, it would be better to use an EntityFieldQuery here. E.g. searching for the term "Foo" in vocabulary 1:
$query = new EntityFieldQuery;$result = $query
->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('name', 'Foo')
->propertyCondition('vid', 1)
->execute();
output of this
PermalinkThanks It helps
output of this is
Array(
[taxonomy_term] => Array
(
[5] => stdClass Object
(
[tid] => 5
)
)
)
taxonomy_get_term_by_name does an case-insensitive search
Permalinktaxonomy_get_term_by_namedoes an case-insensitive search.For example, term name is "MyTerm", then if you give
taxonomy_get_term_by_name('myterm'), it will return taxonomy term object.However, your query will not return any entity.
thanks foobar, that works!
Permalinkthanks foobar, that works!
Second parameter not working in D7
PermalinkJust in case you try to use the second $vocabulary parameter in D7, it wasn't added until 7.14 http://drupal.org/node/1558424