Problem description
When a field contains an invalid reference, the message states
There are no entities matching "invalid reference"
This is a UI/UX issue for the following reasons:
- Normal users (i.e. non-developers) have absolutely no idea what an "entity" is, and therefore this message is meaningless to them
- It does not state which field is causing the error
Proposed resolution
Error messages should be easy to understand and act upon.
In the above case the user-facing messages should be something like:
Invalid value(s) (invalid value 1, invalid value 2, ...) chosen for field_name_1.
Invalid value(s) (invalid value 4, invalid value 5, ...) chosen for field_name_2.
If we wanted to be even extra user-friendly, we should also specify the type of the field (and allow corresponding setting in the entity configuration page), similar to how it's possible to change the text of the Entity Browser buttons, so the message can say:
Invalid term (invalid value 1, invalid value 2, ...) chosen for field_name_1.
Invalid Category (invalid value 4, invalid value 5, ...) chosen for field_name_2.
Invalid Post (invalid value 4, invalid value 5, ...) chosen for field_name_3.
etc.
As it was pointed by @alexpott in #34, we can do the following:
One thing we can do to improve this message is use the entity type label information. We'll need to get the type from the entity type manager. I think \Drupal::entityTypeManager()->getDefinition($element['#target_type'])->getPluralLabel() might work.
User interface changes
Entity reference validation messages will contain entity type plural label instead of "entities".
Comment | File | Size | Author |
---|---|---|---|
#28 | 3138631-interdiff-23_28.txt | 2.38 KB | Matroskeen |
#28 | 3138631-28.patch | 8.89 KB | Matroskeen |
Issue fork drupal-3138631
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:
- 3138631-improve-entity-reference changes, plain diff MR !94
Comments
Comment #2
z.stolar CreditAttribution: z.stolar commentedComment #3
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedFixed the error message, added field name with the wrong value for more clarity.
Comment #5
pavnish CreditAttribution: pavnish at Srijan | A Material+ Company for Drupal India Association commentedComment #6
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedFixed failed test case.
Comment #8
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedComment #9
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedI tried to change the error message 'There are no entities matching "invalid reference"' to 'Invalid value "value" chosen for "field_name"'. I made a change in the EntityAutocomplete file by adding the name of the field entity reference. In method 'matchEntityByTitle' $element is a form element i.e entity reference field. So I used it's title attribute to get the label of the field.
There are 4 test cases that need to be updated. While fixing test cases I realized there can be entity reference field without the label. In one of the cases, entity reference field is used as an exposed filter without a label so test case failed. I could think of one solution as if a title is available then I used a new error message with a field name in it and if not then using the previous error. What can be the better solution for this case when the title for the field is not available? Also, will it be good if I try using the machine name of the field instead of title? Please suggest.
Comment #10
z.stolar CreditAttribution: z.stolar commentedThank you @snehalgaikwad for your work!
IMO, when a label is not available on exposed filters or a field, it usually still has a label in the field's configuration. You should use this one and fall back to machine name if necessary.
Does that respond to your question?
Comment #11
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedHi @z.stolar, I tried finding a label for field but couldn't get. In matchEntityByTitle() method in EntityAutocpmplete file, element is just an array. Couldn't find anything related to a label. Here attaching element variable, both with title and without title.
With title:
Without title
Comment #12
z.stolar CreditAttribution: z.stolar commentedPatch at #9 works very well for me.
Comment #13
z.stolar CreditAttribution: z.stolar at OpenideaL - ideas and innovation management software commentedComment #14
z.stolar CreditAttribution: z.stolar at OpenideaL - ideas and innovation management software commentedComment #15
alexpottI don't think we should be including the field name in the error text - this would be incorrect for inline form errors module - for example. And also we don't do this for other form errors.
One thing we can do to improve this message is use the entity type label information. We'll need to get the type from the entity type manager. I think
\Drupal::entityTypeManager()->getDefinition($element['#target_type'])->getPluralLabel()
might work.Comment #16
pavnish CreditAttribution: pavnish at Srijan | A Material+ Company for Drupal India Association commented@alexpott I have checked #9 with "Inline Form Errors" module it's working fine.
Comment #17
shetpooja04 CreditAttribution: shetpooja04 at QED42 commentedI am working on it
Comment #18
shetpooja04 CreditAttribution: shetpooja04 at QED42 for Drupal India Association commentedUploaded patch as per the suggestion in #15, Please review
Comment #19
shetpooja04 CreditAttribution: shetpooja04 at QED42 commentedComment #21
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedComment #22
Berdirplaceholder is still field name now which doesn't make sense and the tests need to be updated to match what we display now.
Comment #23
Matroskeen1) Applied changes according to #15 and adjusted tests as was pointed in #22;
2) Also, added labels for other types of validation messages in
EntityAutocomplete.php
;I didn't find test for the following message:
"Many @entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"."
Should we add a test coverage for this particular case?
Comment #24
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedComment #25
MatroskeenUpdated the issue summary and title.
Is there anything else we can do to move the issue forward?
Comment #26
MatroskeenComment #27
amateescu CreditAttribution: amateescu as a volunteer commentedCan we rename the
@entities
parameter to@entity_type_plural
for more clarity?Other than that, the patch looks really good to me :)
Comment #28
MatroskeenThanks, here is the updated patch.
Comment #29
MatroskeenComment #30
MatroskeenChanging the component, because there are no changes in
entity_reference
module.Comment #31
amateescu CreditAttribution: amateescu as a volunteer commentedLooks ready to go!
Comment #32
z.stolar CreditAttribution: z.stolar commentedThank you everyone who participated in getting this one through!
Comment #33
catchThis looks great, but needs a re-roll.
Comment #34
MatroskeenWill open merge request soon.
Comment #36
MatroskeenThe merge request with re-roll is open.
Moving back to RTBC as per #31.
Comment #38
catchCommitted f0a40c9 and pushed to 9.2.x. Thanks!
Comment #41
jibranUpdating the tags as it broke DER HEAD as well.