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

StatusFileSize
new5.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 ]
new4.29 KB
FAILED: [[SimpleTest]]: [MySQL] 34,005 pass(es), 2 fail(s), and 0 exception(es).
[ View ]

Status:Active» Needs review

Issue summary:View changes

Updated issue summary.

Issue summary:View changes

Updated issue summary.

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.

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

#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.

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

Status:Needs review» Needs work

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

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

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

Issue summary:View changes

Updated summary.

Issue summary:View changes

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