Posted by xjm

Problem/Motivation

(From #1050466: The taxonomy index should be maintained in a node hook, not a field hook.)

  • If a node has multiple taxonomy autocomplete fields that use the same vocabulary and the user enters the same value in two fields, the term will be duplicated.

Example scenario

This is not just a weird edge case. Consider:

  1. You have a "University" vocabulary.
  2. You allow users to enter their undergraduate school in one field, and their graduate school in another.
  3. The user enters "University of Texas" in both fields.
  4. When the node is saved, there will be two different terms named "University of Texas."

Proposed resolution

  • Check for existing records with the same name in the same vocabulary before creating new terms.

Remaining tasks

  • It's been suggested that the query used in taxonomy_field_presave()to check for existing terms could lead to race conditions:
    +++ b/core/modules/taxonomy/taxonomy.moduleundefined
    @@ -1710,6 +1710,15 @@ function taxonomy_rdf_mapping() {
    +      // 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;
    +      }
    

    See #1050466-34: The taxonomy index should be maintained in a node hook, not a field hook for some proposed solutions.

User interface changes

  • None.

API changes

  • None.
Files: 
CommentFileSizeAuthor
#9 D7-autocreate-1343822-9-notests-do-not-test.patch886 bytesjames.williams
#1 autocreate-1343822-tests.patch4.29 KBxjm
FAILED: [[SimpleTest]]: [MySQL] 34,005 pass(es), 2 fail(s), and 0 exception(es). View
#1 autocreate-1343822-combined.patch5.18 KBxjm
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch autocreate-1343822-combined.patch. Unable to apply patch. See the log in the details link for more information. View

Comments

xjm’s picture

FileSize
5.18 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch autocreate-1343822-combined.patch. Unable to apply patch. See the log in the details link for more information. View
4.29 KB
FAILED: [[SimpleTest]]: [MySQL] 34,005 pass(es), 2 fail(s), and 0 exception(es). View
xjm’s picture

Status: Active » Needs review
xjm’s picture

Issue summary: View changes

Updated issue summary.

xjm’s picture

Issue summary: View changes

Updated issue summary.

xjm’s picture

Marked #1146744: The tagging widget doesn't check if the same term already exists as duplicate. From that issue:

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.

catch’s picture

I think there's another issue for this somewhere as well, but can't find it at the moment.

fietserwin’s picture

#4; #610076: Add a primary key to the {taxonomy_index} table and prevent duplicates being inserted?

I think that the race condition can be avoided by using a merge query (aka upsert), That is, if that would be a real merge query at database level OR if the 2 separate queries (that are issued directly after each other) are placed in a transaction.

As further work on #610076: Add a primary key to the {taxonomy_index} table and prevent duplicates being inserted is going on, I think this one can be closed as a duplicate. Perhaps the test can be added to the mentioned issue.

kscheirer’s picture

#1: autocreate-1343822-combined.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, autocreate-1343822-combined.patch, failed testing.

xjm’s picture

I'd suggest postponing work on taxonomy autocomplete bugs at least a week or so until we see what direction #1847596: Remove Taxonomy term reference field in favor of Entity reference is going to take.

james.williams’s picture

Here's a port of the patch for Drupal 7, without the accompanying tests. Applies cleanly & fixes the bug for me.

james.williams’s picture

Issue summary: View changes

Updated summary.

xjm’s picture

Issue summary: View changes

Removing myself from the author field so I can unfollow. --xjm

jibran’s picture

Component: taxonomy.module » entity_reference.module

All the taxonomy field related matters are handled by entityreference field after #1847596: Remove Taxonomy term reference field in favor of Entity reference

amateescu’s picture

Component: entity_reference.module » taxonomy.module

Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection is provided by the Taxonomy module, moving back to the right queue.

anrikun’s picture

@james.williams
Thanks for patch at #9!

michaelmallett’s picture

Patch worked great for me, thanks. Is there any way to get this in core? Just wondering why it's been around for 3 years.

amateescu’s picture

Title: Autocreated taxonomy terms duplicated if they are added to multiple fields. » Add duplicate term names prevention as a vocabulary option
Version: 8.0.x-dev » 8.1.x-dev
Category: Bug report » Feature request

Since there is no more dedicated taxonomy reference field, just the generic entity reference one, this can no longer be fixed at the field level, we need to move the fix higher up the chain to taxonomy term entity presave.

But, as far as I see, there is no mechanism that prevents adding terms with the same name in a certain vocabulary, so maybe this should be added as a vocabulary configuration option?

With that in mind, I'm moving the issue to 8.1.x and reclassifying as a feature request.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

toni4i’s picture

I am wondering if there is any movement within this issue or maybe even a patch for 8.2.2?

My editors can add multiple event dates to a node using a paragraphs type which also includes a taxonomy reference field for the type of each individual event (for example "concert"). Field settings are autocomplete tagging and "Create referenced entities if they don't already exist".

If they add multiple instances of this paragraph on node creation, all containing a similar, non existent tag of "event type", this term will be created multiple times with the same name, but varying TIDs, after saving the node.

Unfortunately I cannot help with the integration, but I would think the existence of the entered terms has to be validated, maybe for each individual instance of the taxonomy field, one after another, if it appears multiple times inside the node form.