Problem/Motivation
Right after installing Media (or: Taxonomy etc.) module, adding a Media (or Term reference) field to some entity type fails as there's no media type (or Vocabulary) to select from.
In a way this is to be expected. However, the missing checkboxes widget and the subsequent error message aren't very helpful:

(This is with inline form errors. Same problem slightly worse with form errors just at the top.)
1.) We should make sure at least an empty select is always presented to point the sitebuilder to the problem, before hitting an error message. This would probably hold for all kinds of entity references where target bundles are missing.
2.) This is the first impression the sitebuilder gets of the Media (or: Taxonomy) module. So we should either preinstall some basic media type (or vocabulary), so the module can be used rightaway, or redirect the sitebuilder to admin/structure/media (or: whatever) before they get to the field settings, or at the very least present a link to where they can create a bundle or two.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #32 | drupal-no-bundle-error.png | 68.5 KB | merilainen |
Issue fork drupal-3033652
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
pancho+ screenshot
Comment #3
panchotypo
Comment #4
seanbThis is an issue with entity reference fields. Moving accordingly.
Comment #5
panchoThis is an issue with entity reference fields. Moving accordingly.
To a certain extent, yes, unless we're shipping media module with a preconfigured media type, as we do with content types.
While I suggested "splitting off to a separate issue against entity_reference," it's probably fine if we're moving the whole issue there.
I was surprised not to find a bug filed against entity_reference or taxonomy (where the same should occur, though its users might be slightly more experienced or it might be slightly more obvious to create a taxonomy first), so let this one be the place for a generic solution.
As long as we're not adding strings, the Usability bug should be able to go into 8.6.x, if I'm not wrong.
Comment #6
panchoGeneralized, so it refers to all kinds of target bundles.
Comment #7
panchoHere's a working patch and some screenshots:
Comment #9
phenaproximaThis needs review from the usability team. If you can, come to the next usability meeting (they happen on Tuesdays at, I believe, 9:30 PM Germany time). The hangout link is posted in the #ux Slack channel just beforehand. It's a really good opportunity to get opinions and/or sign-off on patches like these.
Comment #10
phenaproximaOh, and it looks like a reroll is needed as well. :(
Comment #11
panchoNo reroll necessary, it was simply the wrong patch. Here's the correct one.
Same screenshots again:
Also, thanks for your invitation! I hope I can be with you next Tuesday! :)
Comment #13
panchoOne more time... sorry for the noise.
Comment #15
amateescu commentedNote that using a bundle entity type is not a requirement, it's just the standard practice, so
getBundleEntityType()can return NULL. We should wrap this part of the code in anif:)Comment #16
panchoThanks for reviewing!
The whole code block in question is however already wrapped in
if ($entity_type->hasKey('bundle')) {which should suffice. Am I missing something?
I manually tested this with:
and in all cases it worked fine.
Another minor oddity:
+ $url = Url::fromUserInput($collection, ['query' => ['destination' => \Drupal::request()->getRequestUri()]]);I'm adding the current Uri as destination to make sure the user returns to complete setting up the entity reference field once they created a bundle. However, the destination gets lost with the second click. Unsure if there's a way to make it stick or if I should remove it.
For some reason, the particular test that failed in #13, passes locally. Fixed a probably unrelated bug, and am rerunning the test now to see if the the test failure is still there.
Comment #18
amateescu commentedContent entity types can also have bundles that are provided by a
hook_entity_bundle_info()implementation, and not by a config entity type. Really, all that's needed if to wrap that code in a condition that checks for a non-NULL value ofgetBundleEntityType():)About the destination stuff, you could also try this:
$url = Url::fromUserInput($collection, ['query' => \Drupal::destination()->getAsArray()]);Comment #19
pancho1.
Ah okay, I missed that. Thanks for pointing me to it!
That would lead to an insanely long if-clause, unless broken down into several lines. However, if
$bundle_entity_type_idis NULL, this will also be NULL:$bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);So instead I'm checking for a non-NULL value of
$bundle_entity_type, just one step later. This way, we can also be even more sure it contains a validEntityTypeInterfaceobject to work with.2.
Thanks, this is a nice shortcut I rolled in. However it doesn't make any difference in regard to the destination surviving the next click. Leaving the non-working destination in though, as the user should really be redirected back to where they came from. Either I missed something and there's a proper way to make it work, or there's currently no way to make it work, then we need to solve this generically.
3.
For some reason,
Drupal\Tests\field\FunctionalJavascript\EntityReference\EntityReferenceAdminTest::testFieldAdminHandlerstill tests green locally, while failing with the testbot. I guess this shouldn't be the case and needs further investigation.Comment #28
quietone commentedI tested this on Drupal 10.1.x., minimal install and was able to reproduce the problem. Fox example, for media the error message was 'Media type field is required." but the 'media type' field had no data.
The same thing happens with comments as well. I am adding a related issue, which may be a duplicate. I am not sure.
This needs an issue summary update, adding tag.
Comment #32
merilainen commentedThis changes the message to be an error displayed on the form element. See the attachment how it looks like.
Comment #33
tomimikola commentedNice improvement! Clear visual indication now for the missing bundles. Tested and works as expected, code looks ok too.
The usability seems to much better now but leaving this up to the committer if additional usability review is needed or not. For me this already improves usability quite much.
Comment #34
xjmThe MR is failing tests and not mergeable. Thanks!
Comment #35
xjmI also don't see any record of the usability review happening. @TomiMikola, please leave it up to the usability team and maintainers as to whether a usability review is required. Issues should not be marked RTBC when a usability review has been requested but has not happened yet.
In order to prepare for the usability team reviewing this, the MR should be gotten into a state where it applies and passes tests, and the issue summary should be updated with screenshots of the current "before" and "after" (both of which should be updated to show 11.x HEAD, since this issue is 5 years old).
Finally, we should investigate the relationship between this issue and the one @quietone linked.
Thanks everyone!