This is more of a workflow issue than a bug.

I have a product type that has size and colour. Both are term references, because I don't want to assume a list of sizes and colours these two fields are term references linked to a "Colours" taxonomy and a "Sizes" taxonomy (autocomplete widget).

If the user is uploading a product like so:
Red, Small
Red, Medium
Blue, Small

... the term "Red" will be added twice in the Colours taxonomy and the dropdown lists on the product display page won't work as expected.

My solution is to save the entity early from within inline_entity_form_process_entity_form(). The variable $context that is passed to save() is only used to name the product, for my purposes I've left it blank because the "late" saving function (when the user saves the product display node) will take care of that naming.

My workaround is pretty crude but this seems like it would be a common use case. Perhaps this could be working into the module with a config option to save referenced entities instantly or only when the parent entity is saved.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

er_d0s’s picture

FileSize
513 bytes
vasike’s picture

Title: Duplicate taxonomy terms for products with multiple variations (when variations are terms) » Term multiplication for new Term values added for Term fields with autocomplete widget on multiple references fields

i can confirm this issue
Term multiplication for new Term values added for Term fields with autocomplete widget on multiple references fields.
This happens because of taxonomy autocomplete widget implementation
https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/funct...
&
https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/funct...

i do not know what to say about solution suggested by previous patch/post.

the only thing i can think of, is a custom validation for taxonomy term reference fields that have the autocomplete widget or 'autocreate' tid defined before saving the Entity from IEF.

any other suggestions, ideeas?

zestagio’s picture

Hi, I encountered the same problem. To solve this problem, I wrote a small module with this code:

function hook_field_attach_presave($entity_type, $entity) {
  // Change $entity->vid == 6 to your vocabulary id.
  if ($entity_type == 'taxonomy_term' && $entity->vid == 6) {
    $term = db_select('taxonomy_term_data', 't')
      ->fields('t', array('tid', 'name'))
      ->condition('t.vid', $entity->vid)
      ->condition('t.name', drupal_strtolower(trim($entity->name)))
      ->execute()
      ->fetchAssoc();

    if (!empty($term)) {
      $entity->name = $term['name'];
      $entity->tid = $term['tid'];
    }
  }
}
vasike’s picture

Status: Active » Needs review
FileSize
1.38 KB

@zestagio : nice solution you found - an extra validation just before saving.
thank you Sir.

here is patch that use your approach and which seems to solve this issue.

andypost’s picture

Project: Inline Entity Form » Drupal core
Version: 7.x-1.x-dev » 8.x-dev
Component: Code » entity_reference.module
Status: Needs review » Needs work

This is a core bug of autocomplete widget, so should be fixed in 8.x first and then backported to 7.x

andypost’s picture

FileSize
7.73 KB

Steps to reproduce:
1) add another Tags2 term reference field to article node type
2) visit /node/add/article
3) enter 'test' in Tags field and the same 'test' into Tags2 field
4) save article
5) now each field value has different terms assigned but with the same name 'test'

expected result - only one term 'test' should be added to Tags vocabulary and both fields should point to that one

terms.png
test2 term points to different terms

vasike’s picture

indeed.
do you want to take care of this or may I?

any suggestions about the implementation?
should be in taxonomy_field_validate() or in taxonomy_field_presave() or other place?

amateescu’s picture

Project: Drupal core » Inline Entity Form
Version: 8.x-dev » 7.x-1.x-dev
Component: entity_reference.module » Code
Status: Needs work » Needs review

I don't see how this is a bug in Entity reference. Even core's taxonomy UI lets you add different terms with the same name, so this is really not our job to 'fix'.. because there's nothing to fix.

er_d0s’s picture

Not that I've been anywhere near this issue since I originally created the ticket, but...

Nothing to fix? In the use case described below the user ends up having the option in a dropdown to choose between a Red or a Red product, if not a bug it's definitely a workflow or conceptual issue. If red was already in the taxonomy the autotagging function would choose the existing term.

amateescu’s picture

Ok, I need to expand my comment: .. nothing to fix in Entity reference, or Term reference for that matter.

As long as taxonomy.module allows terms with the same name, what you describe has to be fixed by adding that unique name check to a vocabulary.

er_d0s’s picture

Gotcha

Having autocomplete there is a nice feature but maybe standard autocomplete isn't the best solution. Maybe something that extends autocomplete.. This one really feels like a chicken and egg problem to me :P

andypost’s picture

Project: Inline Entity Form » Drupal core
Version: 7.x-1.x-dev » 8.x-dev
Component: Code » entity_reference.module
Status: Needs review » Needs work
Issue tags: +Needs tests

@amateescu Please read my example. Autocomplete widget should be fixed (probably for taxonomy) because it allows to add same term twice.
It's not about adding same term twice - it's about logic for autocomplete which checks existence of item only once that leads to erroneous addition.
Proper implementation should not allow duplication and after you re-save the node second term would be lost (autocomplete will find first term and replace Tags2 value with it)

andypost’s picture

I think 'autocreate' assignment should be moved to taxonomy_field_presave() from taxonomy_field_validate() or taxonomy_field_presave() should check for newly created value again.

Also probably entity reference has the same issue

vasike’s picture

Component: entity_reference.module » taxonomy.module
Status: Needs work » Needs review
FileSize
2.98 KB

i agree that Term reference field with autocomplete widget doesn't work properly.
About Entity Reference fields, it should be other issue.

about this issue as D8 core issue, here is a patch - save the same term for the same vocabulary only once..
i also remove "autocreate", as there's no more needed.

andypost’s picture

@vasike great! now needs some tests

andypost’s picture

Issue summary: View changes

Fixing wording

jhedstrom’s picture

This is essentially a reroll of #14, although the logic has completely moved. I've also added a test, and included just the test to show that it fails without this fix.

jhedstrom’s picture

Oops, critical logic error in #16.

The last submitted patch, 16: 1723864-16-term-multiplication.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 17: 1723864-17-term-multiplication.patch, failed testing.

The last submitted patch, 16: 1723864-test-only.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
1.28 KB
4.98 KB

The fix above breaks preview functionality. I've rolled a patch that gets those tests working again, but doesn't fix the issue at hand. Setting to needs review to re-run tests (that will fail).

Status: Needs review » Needs work

The last submitted patch, 21: 1723864-21-term-multiplication.patch, failed testing.

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

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should 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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tea.time’s picture

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Closed (cannot reproduce)

Very similar to https://www.drupal.org/project/drupal/issues/1343822 but think slightly different (least from the description)

Closing as cannot reproduce in Drupal 9.5

Added a taxonomy field. Added red, red and verified red was only saved once.

If you still feel this is an issue please reopen with an update issue summary and steps to reproduce.