Issue #336697 by oriol_e9g, xjm, jbomb, Davy Van Den Bremt, coltrane, lyricnz: Optional vid argument for taxonomy_get_term_by_name().
parent
8cc5ede8dc
commit
e4eae7e7ad
|
@ -1103,12 +1103,25 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities
|
||||||
*
|
*
|
||||||
* @param $name
|
* @param $name
|
||||||
* Name of the term to search for.
|
* Name of the term to search for.
|
||||||
|
* @param $vocabulary
|
||||||
|
* (optional) Vocabulary machine name to limit the search. Defaults to NULL.
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* An array of matching term objects.
|
* An array of matching term objects.
|
||||||
*/
|
*/
|
||||||
function taxonomy_get_term_by_name($name) {
|
function taxonomy_get_term_by_name($name, $vocabulary = NULL) {
|
||||||
return taxonomy_term_load_multiple(array(), array('name' => trim($name)));
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -839,6 +839,34 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase {
|
||||||
// Try to load the term using a substring of the name.
|
// Try to load the term using a substring of the name.
|
||||||
$terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2));
|
$terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2));
|
||||||
$this->assertFalse($terms);
|
$this->assertFalse($terms);
|
||||||
|
|
||||||
|
// Create a new term in a different vocabulary with the same name.
|
||||||
|
$new_vocabulary = $this->createVocabulary();
|
||||||
|
$new_term = new stdClass();
|
||||||
|
$new_term->name = $term->name;
|
||||||
|
$new_term->vid = $new_vocabulary->vid;
|
||||||
|
taxonomy_term_save($new_term);
|
||||||
|
|
||||||
|
// Load multiple terms with the same name.
|
||||||
|
$terms = taxonomy_get_term_by_name($term->name);
|
||||||
|
$this->assertEqual(count($terms), 2, t('Two terms loaded with the same name.'));
|
||||||
|
|
||||||
|
// Load single term when restricted to one vocabulary.
|
||||||
|
$terms = taxonomy_get_term_by_name($term->name, $this->vocabulary->machine_name);
|
||||||
|
$this->assertEqual(count($terms), 1, t('One term loaded when restricted by vocabulary.'));
|
||||||
|
$this->assertTrue(isset($terms[$term->tid]), t('Term loaded using exact name and vocabulary machine name.'));
|
||||||
|
|
||||||
|
// Create a new term with another name.
|
||||||
|
$term2 = $this->createTerm($this->vocabulary);
|
||||||
|
|
||||||
|
// Try to load a term by name that doesn't exist in this vocabulary but
|
||||||
|
// exists in another vocabulary.
|
||||||
|
$terms = taxonomy_get_term_by_name($term2->name, $new_vocabulary->machine_name);
|
||||||
|
$this->assertFalse($terms, t('Invalid term name restricted by vocabulary machine name not loaded.'));
|
||||||
|
|
||||||
|
// Try to load terms filtering by a non-existing vocabulary.
|
||||||
|
$terms = taxonomy_get_term_by_name($term2->name, 'non_existing_vocabulary');
|
||||||
|
$this->assertEqual(count($terms), 0, t('No terms loaded when restricted by a non-existing vocabulary.'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue