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.
Comment | File | Size | Author |
---|---|---|---|
#21 | 1723864-21-term-multiplication.patch | 4.98 KB | jhedstrom |
#21 | interdiff.txt | 1.28 KB | jhedstrom |
#17 | 1723864-17-term-multiplication.patch | 5 KB | jhedstrom |
#17 | interdiff.txt | 818 bytes | jhedstrom |
#16 | 1723864-16-term-multiplication.patch | 4.97 KB | jhedstrom |
Comments
Comment #1
er_d0s CreditAttribution: er_d0s commentedComment #2
vasikei 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?
Comment #3
zestagio CreditAttribution: zestagio commentedHi, I encountered the same problem. To solve this problem, I wrote a small module with this code:
Comment #4
vasike@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.
Comment #5
andypostThis is a core bug of autocomplete widget, so should be fixed in 8.x first and then backported to 7.x
Comment #6
andypostSteps to reproduce:
1) add another
Tags2
term reference field to article node type2) 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
test2
term points to different termsComment #7
vasikeindeed.
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?
Comment #8
amateescu CreditAttribution: amateescu commentedI 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.
Comment #9
er_d0s CreditAttribution: er_d0s commentedNot 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.
Comment #10
amateescu CreditAttribution: amateescu commentedOk, 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.
Comment #11
er_d0s CreditAttribution: er_d0s commentedGotcha
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
Comment #12
andypost@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)
Comment #13
andypostI 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
Comment #14
vasikei 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.
Comment #15
andypost@vasike great! now needs some tests
Comment #15.0
andypostFixing wording
Comment #16
jhedstromThis 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.
Comment #17
jhedstromOops, critical logic error in #16.
Comment #21
jhedstromThe 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).
Comment #29
tea.time CreditAttribution: tea.time commentedI think this is a duplicate of #1343822: Autocreated taxonomy terms duplicated if they are added to multiple fields and is outdated.
Comment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedVery 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.