diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index eb20c26..9b490d8 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -1710,6 +1710,15 @@ function taxonomy_rdf_mapping() { function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $item) { if ($item['tid'] == 'autocreate') { + // Avoid duplicating tags within the same vocabulary. + $tid = db_query_range("SELECT tid FROM {taxonomy_term_data} WHERE name = :name AND vid = :vid", 0, 1, array( + ':name' => trim($item['name']), + ':vid' => $item['vid'], + ))->fetchField(); + if (!empty($tid)) { + $items[$delta]['tid'] = $tid; + continue; + } $term = (object) $item; unset($term->tid); taxonomy_term_save($term); diff --git a/core/modules/taxonomy/taxonomy.test b/core/modules/taxonomy/taxonomy.test index 747d822..bbb5df5 100644 --- a/core/modules/taxonomy/taxonomy.test +++ b/core/modules/taxonomy/taxonomy.test @@ -829,6 +829,124 @@ class TaxonomyTermTestCase extends TaxonomyWebTestCase { } /** + * Tests term autocreation with multiple autocomplete widgets. + */ +class TaxonomyTermAutocreateTestCase extends TaxonomyWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Taxonomy term autocreation', + 'description' => 'Tests term autocreation with multiple autocomplete widgets.', + 'group' => 'Taxonomy', + ); + } + + function setUp() { + parent::setUp('taxonomy'); + // Create an administrative user. + $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); + $this->drupalLogin($this->admin_user); + + // Create a vocabulary and add two autocomplete fields on article nodes. + $this->vocabulary = $this->createVocabulary(); + + $this->field_name_1 = drupal_strtolower($this->randomName()); + $this->field_1 = array( + 'field_name' => $this->field_name_1, + 'type' => 'taxonomy_term_reference', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $this->vocabulary->machine_name, + 'parent' => 0, + ), + ), + ), + ); + field_create_field($this->field_1); + $this->instance_1 = array( + 'field_name' => $this->field_name_1, + 'bundle' => 'article', + 'entity_type' => 'node', + 'widget' => array( + 'type' => 'taxonomy_autocomplete', + ), + 'display' => array( + 'default' => array( + 'type' => 'taxonomy_term_reference_link', + ), + ), + ); + field_create_instance($this->instance_1); + + $this->field_name_2 = drupal_strtolower($this->randomName()); + $this->field_2 = array( + 'field_name' => $this->field_name_2, + 'type' => 'taxonomy_term_reference', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $this->vocabulary->machine_name, + 'parent' => 0, + ), + ), + ), + ); + field_create_field($this->field_2); + $this->instance_2 = array( + 'field_name' => $this->field_name_2, + 'bundle' => 'article', + 'entity_type' => 'node', + 'widget' => array( + 'type' => 'taxonomy_autocomplete', + ), + 'display' => array( + 'default' => array( + 'type' => 'taxonomy_term_reference_link', + ), + ), + ); + field_create_instance($this->instance_2); + } + + /** + * Tests adding the same term to multiple autocomplete fields. + */ + function testTaxonomyTagging() { + $term1_name = $this->randomName(); + + // Post an article. + $edit = array(); + $langcode = LANGUAGE_NONE; + $edit["title"] = $this->randomName(); + $edit["body[$langcode][0][value]"] = $this->randomName(); + $edit["{$this->field_name_1}[$langcode]"] = $term1_name; + $this->drupalPost('node/add/article', $edit, t('Save')); + + // Check that the term is saved and can be retrieved. + $terms = taxonomy_get_term_by_name($term1_name); + $this->assertEqual(1, count($terms), t('The term was saved.')); + + // Add a new term to two different fields. + $node = $this->drupalGetNodeByTitle($edit["title"]); + $term2_name = $this->randomName(); + $edit["{$this->field_name_1}[$langcode]"] = $term2_name; + $edit["{$this->field_name_2}[$langcode]"] = $term2_name; + $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); + + // Check that the term is displayed multiple times. + $this->assertText($term2_name, t('The term was saved and appears on the node page.')); + $this->assertNoUniqueText($term2_name, t('The term displays multiple times.')); + + // Check that the term is saved only once. + $terms = taxonomy_get_term_by_name($term2_name); + $this->assertEqual(1, count($terms), t('The term was saved only once.')); + } +} + +/** * Test the taxonomy_term_load_multiple() function. */ class TaxonomyLoadMultipleUnitTest extends TaxonomyWebTestCase {