Problem/Motivation
Follow-up for #2174633: View output is not used for entityreference options: This covers the display of items previously selected in an autocomplete text field.
When the options for selection in an entity auto-complete relation text field are controlled via a view, the view fields are not used to display the selected entity on subsequent edits of the same entity; instead, the entity label is shown. This is a regression from Drupal 7.x.
Expected Results
In step 6, the text in the field should match what was configured in the view, the same way as it appeared when the selection was made in step 4.
Actual Results
In step 6, the text in the field is equal to the entity label, even though that's not the value that was in the field when the selection was made in step 4.
Steps to reproduce
- Create a view with an entity reference display and set it up to display more than one field as an inline field.
- Add an entity reference field using the auto-complete text field, using the entity reference view display, to a content type.
- Visit the node add form.
- Fill out the field that was added in step 2; this will show the selected entity as configured in the View (using the selected inline fields).
- Save the node
- Open the edit back up for editing.
- Look at the value that appears in entity reference field that was populated in step 4.
Proposed resolution
TBD
Remaining tasks
Update summary
Clean up MRs
User interface changes
NA
API changes
TBD
Data model changes
TBD
Release notes snippet
NA
Comment | File | Size | Author |
---|
Issue fork drupal-2796341
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jonathanshawI'm might be up for making a start with this, but I need some guidance.
You say this is a regression from D7, could you point to the code in D7 that enables this? I scratched around in the entity_reference module and saw nothing obvious, but my understanding of D7 is very thin.
For D8, my first guess would be to modify the entity reference widget. Basically modify the formElement method so that each time it is called it checks the delta to see if it's the last (i.e. new value field item). If it is the last, then the code should work as it currently does. If it is not the last (i.e. an existing value) then it should check to see if we're using the views handler, and if we are then instead of the current code to build a form element, it should render a single row view instead. I'm very vague around what this last point means and how to do it.
However, that seems very invasive to the entity reference widget, which rather defeats the point of having selection handlers as plugins. Maybe it would be better to add an additional "renderableSelectionHandlerInterface" to the selection handlers. The widget would then see if the handler pluginimplemented that interface, and if it did call a "buildItem" method on it the handler plugin; if it did not (or if that returned null) it would fall back to building its current form element.
Comment #3
imclean CreditAttribution: imclean commentedcore/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
looks like a good start.valueCallback()
callsgetEntityLabels()
, when it could check for views under "#selection_settings" and process that accordingly.For some example views code, see the interdiff in post 70.
Comment #4
imclean CreditAttribution: imclean commentedHere's a start. It takes into account multiple results for a single field separated by commas.
I'm not sure if this is the right approach so there aren't any tests.
Comment #5
imclean CreditAttribution: imclean commentedComment #6
imclean CreditAttribution: imclean commentedA selection handler would be more flexible.
Comment #7
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedYes, ideally
EntityAutocomplete::getEntityLabels()
would invoke a method on the selection handler, because only it knows how to properly format its output.Comment #8
jonathanshawIn which case should we create a method with a more generic name like getEntityDisplay, and deprecate getEntityLabels?
On a separate subject, perhaps code here should build on the reorganisation in #2787873: Add a base class for entity reference selection handlers and fix the structure of their configuration which is quite well advanced.
Comment #9
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedI wouldn't deprecate
getEntityLabels()
but add an additional optional argument to it, for exampleSelectionInterface $selection_handler = NULL
, which, if given, will get the output from the passed-in selection through agetAutocompleteText()
method.. or something like that.Comment #11
imclean CreditAttribution: imclean commentedWe're still using the patch in #4 with Drupal 8.2.x which is working well. When #2787873: Add a base class for entity reference selection handlers and fix the structure of their configuration is in we may revisit this if we're planning to update the relevant site to 8.4.
Comment #15
C.E.A CreditAttribution: C.E.A commentedI am encountering two issues about using entity reference view display in Drupal 8.6.1:
1) At the
/node/[nid]/edit
page, the select list widget for entity reference view display is showing only the username of the current logged in user for all the available options... and this is almost fixed with this patch (https://www.drupal.org/project/drupal/issues/2174633) and it is working for me !!2) At the
/node/[nid]
page, the output of the select list entity reference view is broken and displaying only the username of the logged in user and referencing the user page as well !I have been redirected to this issue as it will sove my problem, but after i applied the patch #4, the problem still exist and nothing has really changed !
And also I see that the last update on this issue was 2 years ago so am i on the right track or I am missing something here !?
Comment #16
jonathanshawI don't think this issue will solve your problem. I think this issue is about what is shown in a widget or other form element e.g. at node /[nid]/edit for already selected items.
#2174633: View output is not used for entityreference options covers what is show in the selector when selecting new items, this covers the display ON THE FORM of already selected items.
In general to control what appears at node/[nid] you want to use the 'Manage display' not the 'Manage form display UI', and in particular you would normally use a 'Rendered entity' formatter not the 'Label' formatter.
However, if it's node author you want to mess with you're out of luck, because that's got weird legacy issues. You may well find it easiest just to hack what you want into your node theme template, it all depends on your exact use case and I suggest you try Drupal Answers if you want help with it.
Comment #17
C.E.A CreditAttribution: C.E.A commentedI am aware already about the use of the "manage display" and not the "form display" but your mentioning about the "render entity" formatter put me again on the right track and now all is working as expected !
Or at least until this issue will be officially fixed !
Thank you once again, god bless,
Comment #18
philipsoares CreditAttribution: philipsoares commentedI added new treatments.
When the autocomplete field is not a style tag, you do not need to enclose the value with double quotation marks.
When we have characters like & in the title, it is transformed into & amp;
To solve I added the html_entity_decode function before the strip_tags
Comment #20
kenton.r CreditAttribution: kenton.r commentedI use the 2796341-18.patch and it works for me. It would be good to get this committed.
Comment #21
colanComment #22
mitchellshelton CreditAttribution: mitchellshelton commentedI applied this patch and it is working as expected.
Just out of curiosity, is there a reason why the Entity ID is appended to the value?
For example, we have this currently:
Why not this instead:
I don't see a way to override this from within views. Perhaps I'm missing something, or just don't understand the history here.
Anyway, thanks for the excellent patch, huge help for me.
Comment #23
jonathanshaw#22 the appended id is to disambiguate entities with identical labels
Comment #25
Dries ArnoldsMaybe we can get this moving now that it's related issue is fixed?
Comment #26
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedYup, it would be good to make some progress here :) See #7 for my thoughts on what the patch should do.
Comment #27
asak CreditAttribution: asak commentedPatch in comment 18 applies smoothly to 8.8.5 and resolves the issue correctly - this is RTBC.
May I suggest any additional changes go into new issues?
Now that #2174633 is committed this will be a nice issue to resolve as well.
Comment #28
Neslee Canil PintoPath for 8.9.x
Comment #29
anruetherClarified what this issue is about in the summary according to #16
Comment #31
danharper CreditAttribution: danharper as a volunteer and at hrpr commentedI have tested this with 8.9.x and it seems to work.
Comment #32
larowlanThanks, this looks like the patch is long out of date, e.g. functions like drupal_set_message are gone in 9.x, and old array syntax is something we don't use anymore.
In addition we need some test coverage here.
We could also use an issue summary update to use the template, including steps to reproduce etc.
Also, there is no entity reference module anymore, so changing the component
this needs a docblock
this is using old array syntax
drupal_set_message has been removed, this needs to use the messenger service
the indenting is wrong here
Comment #33
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedCreated patch for 9.1.x. Covered fixes addressed in #32.
Comment #34
jonathanshawThanks @snehalgaikwad! Please post an interdiff where possible when working on a patch.
NW for IS update and tests.
Comment #35
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedYes @jonathanshaw. I created patch for 9.1.x version(prev patch was for 8.9.x) with fixes mentioned so didn't add interdiff.
Comment #36
LendudeIS update
Comment #37
LendudeHere is a test for this. Test only patch is the interdiff (ok, one minor change to not get a php notice)
However....see #7/#8/#9 for the right path to fixing this. The current fix puts Views specific code inside the generic EntityAutocomplete handler, this is not what we want.
So this might come back green, but it still needs work.
Comment #38
LendudeLets try that again with patches rolled against 9.1.....
Comment #40
jonathanshawNW per #37 as the fix needs to be fundamentally different
Comment #41
LendudeHere we go with a rewrite. Probably needs a clean-up but lets see what the bots says this breaks.
Comment #42
LendudeWell, that broke less then I expected....
I don't like that the Views code needs to do this. This needs to be done for all labels so we shouldn't have to repeat that logic.
Just not sure to to best refactor for that right now
Comment #43
kybermanThank you! I can confirm patch #28 works fine with 8.9.6.
Comment #44
chr.fritschDo we need the "($entity_id)" in the output of a ViewsSelection?
I think thats only releated to the EntityAutocomplete element. When you are using another element, for example select2, you don't need and want the braces with the entity_id in your results.
Comment #46
westsonoma CreditAttribution: westsonoma commentedThank you! 2796341-41.patch installs and works without issue on Drupal core 9.0.7, and is exactly what I was looking for.
Comment #47
KurtTrowbridgeThanks for this! I'm on 8.9.11 and #28 applied cleanly. I realized after the fact that none of this appears to be relevant to the 9.x patch, but for anyone using 8.9.x, I cleaned up the code standards issues mentioned in #32, fixed a typo, and added the
ENT_QUOTES
argument tohtml_entity_decode()
to prevent entities with straight apostrophes (') from being escaped when printed.Comment #48
anmolgoyal74 CreditAttribution: anmolgoyal74 at OpenSense Labs for DrupalFit commentedFixed minor CS issues in #47.
Comment #49
andypostComment #51
gbotis CreditAttribution: gbotis commentedThanks! Just tested with Drupal 9.2 and works perfectly.
Comment #52
Yury N CreditAttribution: Yury N as a volunteer commentedRe-rolled patch #41 against 9.2.x
Comment #53
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedFixed custom command failure.
Comment #56
Nathaniel CreditAttribution: Nathaniel commented@ #52/#53 looks like a typo "entites" should be "entities".
#48 is working on 9.2. #52/#53 look like a different approach.
Comment #57
andypostNot clear why interface renamed in latest patches, I see no reason for it
It needs proper re-roll and address #42
needs CR
Comment #58
andypostHere's re-roll and small fixes (including CS)
@chr.fritsch re #44 - yes, entity IDs are needed (frontend could hide'em) otherwise there's no way to differentiate same-labeled entities, there's #2881892: UX: Hide entity ID in autocomplete widget
Comment #59
andypostinterdiff is 41 vs 58
Comment #60
andypostFix CS from #53
Comment #61
jonathanshawPlease everyone do not post patches that are not descended from #42 #58 / #60 as this is the only approach that has any chance of being committed, and it gets confusing when two different approaches are being worked on in the same issue.
As far as I can see #42 can be addressed fairly easily in EntityAutocomplete::getEntityLabels().
Comment #63
vasikeComment #64
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedTwo bugs that need to be addressed:
Comment #65
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedI'm going to take a stab at moving #60 over to an issue fork to address this feedback and the feedback from #42.
Comment #66
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedAlso: see #2951134: Single-value autocomplete widgets don't warn about field cardinality, lose data silently if you want non-tag entity reference fields to validate the cardinality of each delta properly. I will not be adding the fix for that here, but it is a complementary fix.
Comment #68
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedOk, I believe I've addressed all the feedback and the two bugs I found. Everything should be in MR 2761. Here is the inter-diff from 60:
https://git.drupalcode.org/project/drupal/-/compare/d4407da8d03dc727d7a5...
Each commit contains a detailed description of the changes I made.
Comment #69
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedSetting back to me to address test failures.
Comment #70
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedHiding older patches.
Comment #71
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedNew inter-diff since #60:
https://git.drupalcode.org/project/drupal/-/compare/d4407da8d03dc727d7a5...
Comment #72
ameymudras CreditAttribution: ameymudras at Salsa Digital commentedI have posted a few suggestions in the MR, please consider if they are relevant
Comment #74
liquidcms CreditAttribution: liquidcms commentedJust to bring this up again as it has been asked a few times in this thread: "why is the (id) appended to the field result value?"
the only reply i see here is in #23: "the appended id is to disambiguate entities with identical labels" - but that's not a good answer. That's a Drupalism and is only a site specific requirement. We have entityref fields accessible to client's. They have no idea what that number means and in many cases, such as for user reference fields, this could even be considered a security issue.
These db id values should not be shown here.
NOTE: i am talking about the result which ends up in the field (not what is shown as options in the autocomplete)
Comment #76
dqdI see all the points and worries but to get forward with this issue and its main focus and primary scope we should prevent to disperse discussion about corners of likes and dislikes in the solution and should minimize the scope as much as possible.
This issue isn't merely important for custom entities and projects like ECK and paragraphs but also for any entity types, which do not have mandatory title (label) field. Also in core, since there are discussions about to change the mandatory title field for nodes too.
EDIT (after further testing): To clarify (also for me) this issues scope here seem to be merely for the use of autocomplete and inline edit reference field formatter and similar form field formatters where you are able to CREATE NEW referenced items. I tested custom entities with select lists and alternative form field widgets like chosen with existing custom entities, and I used Views reference views to build custom Views reference select lists to choose from and the form field preview reflects the selected list item as is. So I assume this should be clarified in the issue summary?
Can somebody who is more into this issue, update the summary and can make some notes about what to do next to get forward so that others can chime in to help? Since this issue is a little bit dated it surely needs updates on the to-do list.
Comment #77
dqdMore investigating and maybe "good to know" information: For those who use inline_entity_form for creating new entities in the progress of adding them as references can use ief_table_view_mode to get a custom preview of the reference in the edit form display. This solves the problem of entities which have no "label" (title) field looking like empty entries in the preview. Thanks to @mnico for this contribution and important extension for the IEF workflow.
Comment #78
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedTaking on additional rework.
Comment #79
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedI reviewed the MR feedback (thank you, @ameymudras). I've ticketed #3423053: Replace \Drupal calls with proper IoC in form elements under the #2729597: [meta] Replace \Drupal with injected services where appropriate in core meta issue. That issue should be appropriately scoped to rework form elements like the Entity Auto-complete text field to use DI. I am moving both
\Drupal::service('plugin.manager.entity_reference_selection')
calls in the class into a utility method and adding a todo that references #3423053. I believe that to be the most prudent way to avoid bloating the scope of this issue.Regarding your feedback, @liquidcms, I agree with what I believe @dqd is saying -- the scope of this issue is to fix what value appears in the autocomplete field when a view is being used to drive selections. The question of whether an ID should be suffixed or not is outside the scope of this issue. The issue you are looking for is #2881892: UX: Hide entity ID in autocomplete widget and until that issue lands, the implementation in this ticket is consistent with both the way that the ID appears during initial selection with a view as well as autocomplete fields that are not driven by a view.
As for your other question, @dqd, it seems like you found a solution to your problem with IEF. Regardless, I have cleaned up the issue description to clarify that this issue is specific to autocomplete text fields. Please let me know if that addresses your issue, since IEF is completely separate.
Comment #80
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedLinking related issues.
Comment #81
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedComment #82
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedJust updated the branch for 9.5. My next update will update this for 10.1.x.
Comment #83
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedUpdated for 10.1.x.
Comment #84
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedUpdated for 11.x. Ready for re-review.
Comment #85
smustgrave CreditAttribution: smustgrave commentedIssue summary appears to be missing a few sections
Added the missing sections but left TBD on sections for someone more familiar with the issue to speak on.
Also can we hide some of the MRs to just have the intended for 11.x branch. Can be backported later depending when this lands.