This problem occurs when a content type has multiple tagging fields using the same vocabulary. In such case, users can enter the same term in different fields which leads to non-unique tagging terms.

Comments

cluster4’s picture

I'm not used to the patching workflow of drupal. So here comes some raw code that can be considered as a quick fix for the described problem.
In "taxonomy.module" jump to the taxonomy_field_presave() function and add the following code to the top of the foreach loop:

      $existing_terms = taxonomy_get_term_by_name($item['name']);
      if (!empty($existing_terms)) {
        $keys = array_keys($existing_terms);
    	$items[$delta]['tid'] = $keys[0];
    	continue;
      }
sun’s picture

Version: 7.0 » 8.x-dev
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs backport to D7

We need a much more clear and concise problem description here. Including clean steps to reproduce.

mr.baileys’s picture

Priority: Normal » Minor
Status: Postponed (maintainer needs more info) » Active

Confirmed this as a bug, steps to reproduce:

  • Create a vocabulary
  • Create a content type
  • Add an autocomplete taxonomy term reference field to the content type
  • Add another autocomplete taxonomy term reference field to the content type
  • Create a node for the content type, using the same term in both term reference fields

Expected outcome: term is created once, and both term reference fields reference that term.
Actual outcome: term is created twice, and both term reference fields refer to a separate term.

Reason seems to be that taxonomy_autocomplete_validate() uses taxonomy_term_load_multiple(), which caches the result. During the first iteration the term does not exist and is created, during the second iteration, taxonomy_term_load_multiple() still considers the term not to exist (cached), even though it was just created.

I can't really think of a valid use case for having 2 taxonomy reference fields (referencing the same vocabulary) on the same content type though, so downgrading this to a minor bug.

One solution might be to start using EntityFieldQuery to fetch the tids to load with taxonomy_term_load_multiple() instead of relying on the deprecated $conditions argument.

fangel’s picture

mr.baileys: Isn't the problem more that the exist-check is done by taxonomy_autocomplete_validate, but the term-creation is done by taxonomy_field_presave, not that the results from taxonomy_term_load_multiple is cached?

So first both autocomplete-fields are element-validated, which sets them as "need to be created", and when the field is then saved, no checking is done to see if the term exists, so both terms are created.

xjm’s picture

Status: Active » Closed (duplicate)

Oops, didn't find this before I opened #1343822: Autocreated taxonomy terms duplicated if they are added to multiple fields, which itself was split off from #1050466: The taxonomy index should be maintained in a node hook, not a field hook. There's a patch there, though, so marking this issue duplicate.