Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
On an autocomplete widget, using the entity's title will not save the entity reference when it should.
EntityReference_SelectionHandler_Generic::getReferencableEntities returns an array structure of
array(
bundle_name => array(
entity_id => entity_label
)
)
while EntityReference_SelectionHandler_Generic::validateAutocompleteInput only counts the number of bundles and not the number of entities because it expects this array structure
array(
entity_id => entity_label
)
Comments
Comment #1
bryllecagadas CreditAttribution: bryllecagadas commentedI should have reworded my previous issue summary but here goes:
When using any of the autocomplete widgets, typing in the entity title should reference the entity if there is only a single entity that has that title without interacting with the dropdown list.
Steps to reproduce:
Add a Entity Reference field for basic pages referencing articles named field_article, with widget type autocomplete.
Add an article having title 'This is an article'.
Add a basic page while putting 'This is an article' in the field_article autocomplete widget without interacting with the dropdown list.
Expected result:
The created basic page should have an entity reference to 'This is an article' article.
Actual result:
The article is not referenced.
Patch is attached.
Comment #2
sammys CreditAttribution: sammys commentedThanks for the patch. I've moved implementation of the fix for this issue into #1472596: Free tagging entity reference to a vocabulary does not create the term, only allows existing terms.
Comment #3
rupertj CreditAttribution: rupertj commentedCould we get this fix looked at separately to #1472596? This issue's a simple little bugfix, and the other one's a pretty chunky feature request now. They don't even seem to be that related. I'll try the patch out and see if it needs a re-roll, as I'm running into the issue described here too.
Comment #4
rupertj CreditAttribution: rupertj commentedJust tried it. Applies cleanly and fixes the issue. Please commit this if testbot agrees.
Comment #5
BerdirCross-referencing #1901594: _entityreference_autocomplete_tags_validate not getting target_id, which also contains a fix for this.
Comment #6
BerdirOk, closed the other one as duplicate, fixed the documentation and wrote some very basic test coverage.
Comment #8
amitaibuLooks good, just small nitpicks:
An nested => A nested
I think t() should be replaced with format_string()
Comment #9
Berdir1. Yep :)
2. Nope, this isn't an assertion message, it's the actual validation error that we're checking with assertEqual(), that has to be t() as the original is using t() too. Just like clickLink() and drupalPost() button labels still use t() in tests.
1 can be fixed on commit I'd say, unless you want a new patch :)
Comment #10
MustangGB CreditAttribution: MustangGB commentedMarked #2185141: Required entity reference is possible to save with invalid, but tricky content. as a duplicate.
Comment #11
MustangGB CreditAttribution: MustangGB commentedThis is also an issue with the views handler.
#1702172: Saving allowed even when input is not valid in autocomplete results
With regards the patch, it works as advertised.
Comment #12
OostiePatch #6 fixes my issue.
Before the patch the validation function returned the entity type and after i after it returned the entity id.
thanks!
Comment #13
MustangGB CreditAttribution: MustangGB commentedThe match_operator '=' gets ignored in favour of the default 'CONTAINS' aka 'LIKE'.
Not sure if this is the best way to fix it, or even if this is the right issue, but I've attached a patch to address this concern.
Comment #15
darrick CreditAttribution: darrick commentedI rerolled the patches in #1 and #6 and made the changes referenced in #8. I believe most of the referenced issues simply confuse the matter. This patch fixes the issue described in comment #1.
Comment #16
ashedryden CreditAttribution: ashedryden commented@darrick - Applied patch from #15 to Entity Reference 7.x-1.1, cleared caches twice, no effect on issue in #1.;
Patch here: https://www.drupal.org/node/1702172#comment-8681155 applied and did solve the effect in issue #1 here.
Comment #17
darrick CreditAttribution: darrick commented@ashedryden - looking at the patches ... mine in #15 fixes the issue when using the default generic select class. I've also applied the patch you mention to fix the issue when using a view for selection.
Comment #18
vflirt CreditAttribution: vflirt commentedIt works fine and solved my issues.
Comment #19
DamienMcKennaComment #20
dawehnerThe patch solved the problem for me perfectly
Comment #21
hgoto CreditAttribution: hgoto as a volunteer and at Studio Umi commentedhttps://www.drupal.org/node/1974202 was closed as duplicate.
Comment #22
MustangGB CreditAttribution: MustangGB commentedThis issue has quite a few duplicates and is also a blocker for other issues so bumping up the priority a bit to reflect this.
Would be great to get some maintainer eyes on.
Comment #23
dbiscalchin CreditAttribution: dbiscalchin commented#15 worked for me too. Please commit.
I raised the priority to critical, because this issue is quite old and has the serious effect of allowing invalid data to be stored. So applications that expect a value for these fields can break. Furthermore, often the person who submitted the form will not notice the problem until her or she gets back to it and tries to submit again.
Comment #24
daxelrod CreditAttribution: daxelrod commentedPatch in #15 works, +1 for commit.
Comment #25
hgoto CreditAttribution: hgoto as a volunteer and at Studio Umi commented#15 works well and the code looks good to me. +1 for commit :)
Comment #27
spotzero CreditAttribution: spotzero at Coldfront Labs Inc. commentedCommitted. Thank you everyone for your patience.
Comment #29
Kris77 CreditAttribution: Kris77 commentedPatch in #15 works for me too