diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index dc2847d..c477b5a 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -876,7 +876,7 @@ function taxonomy_get_parents_all($tid) { * @param $tid * A taxonomy term ID. * @param $vid - * An optional vocabulary ID to restrict the child search. + * (optional) Vocabulary ID to restrict the child search. * * @return * An array of term objects which are the children of the term $tid. @@ -1028,12 +1028,19 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities * * @param name * Name of the term to search for. + * @param vid + * (optional) Vocabulary ID of the vocabulary to limit the search by, + * defaults to all. * * @return * An array of matching term objects. */ -function taxonomy_get_term_by_name($name) { - return taxonomy_term_load_multiple(array(), array('name' => trim($name))); +function taxonomy_get_term_by_name($name, $vid = NULL) { + $conditions = array('name' => trim($name)); + if ($vid) { + $conditions['vid'] = $vid; + } + return taxonomy_term_load_multiple(array(), $conditions); } /** diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index 97cfe44..ac15a34 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -789,6 +789,30 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase { // Try to load the term using a substring of the name. $terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2)); $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, $term->vid); + $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 id.')); + + // 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_term->vid); + $this->assertFalse($terms, t('Invalid term name restricted to vocabulary id not loaded.')); } }