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:

Type missing
(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

Issue fork drupal-3033652

Command icon 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

Pancho created an issue. See original summary.

pancho’s picture

Issue summary: View changes
StatusFileSize
new19.65 KB

+ screenshot

pancho’s picture

Issue summary: View changes

typo

seanb’s picture

Version: 8.6.x-dev » 8.7.x-dev
Component: media system » entity_reference.module

This is an issue with entity reference fields. Moving accordingly.

pancho’s picture

Title: "Media type field is required", but nothing to select from » "type field is required", but no bundle to select
Version: 8.7.x-dev » 8.6.x-dev
Related issues: +#1953568: Make the bundle settings for entity reference fields more visible., +#2348615: Clean up entity reference field settings anomalies, +#180109: Only taxonomy term selected by default when there is only one

This 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.

pancho’s picture

Issue summary: View changes

Generalized, so it refers to all kinds of target bundles.

pancho’s picture

Title: "type field is required", but no bundle to select » Be more helpful if there's no bundle to choose from
Version: 8.6.x-dev » 8.7.x-dev
Status: Active » Needs review
Issue tags: +Usability
StatusFileSize
new2.11 KB
new11.13 KB
new10.4 KB
new11.11 KB

Here's a working patch and some screenshots:

No vocabulary
No media-type
No block-type

Status: Needs review » Needs work

The last submitted patch, 7: 3033652-7.patch, failed testing. View results

phenaproxima’s picture

Issue tags: +Needs usability review

This 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.

phenaproxima’s picture

Issue tags: +Needs reroll

Oh, and it looks like a reroll is needed as well. :(

pancho’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new1.99 KB

No reroll necessary, it was simply the wrong patch. Here's the correct one.

Same screenshots again:

No vocabulary
No media-type
No block-type

Also, thanks for your invitation! I hope I can be with you next Tuesday! :)

Status: Needs review » Needs work

The last submitted patch, 11: 3033652-11.patch, failed testing. View results

pancho’s picture

Status: Needs work » Needs review
StatusFileSize
new1.16 KB
new2.04 KB

One more time... sorry for the noise.

Status: Needs review » Needs work

The last submitted patch, 13: 3033652-13.patch, failed testing. View results

amateescu’s picture

+++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php
@@ -185,6 +186,25 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
+        $bundle_entity_type_id = $entity_type->getBundleEntityType();

Note 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 an if :)

pancho’s picture

Status: Needs work » Needs review
StatusFileSize
new754 bytes
new2.02 KB

Thanks 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:

  • content entity types that are defining a bundle entity type
    • with existing bundles
    • without existing bundles
  • (custom) content entity types that aren't defining a bundle entity type
  • config entity types

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.

Status: Needs review » Needs work

The last submitted patch, 16: 3033652-16.patch, failed testing. View results

amateescu’s picture

The whole code block in question is however already wrapped in
if ($entity_type->hasKey('bundle')) {
which should suffice. Am I missing something?

Content 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 of getBundleEntityType() :)

About the destination stuff, you could also try this:

$url = Url::fromUserInput($collection, ['query' => \Drupal::destination()->getAsArray()]);

pancho’s picture

StatusFileSize
new1.58 KB
new2.02 KB

1.

Content entity types can also have bundles that are provided by a hook_entity_bundle_info() implementation, and not by a config entity type.

Ah okay, I missed that. Thanks for pointing me to it!

Really, all that's needed if to wrap that code in a condition that checks for a non-NULL value of getBundleEntityType() :)

That would lead to an insanely long if-clause, unless broken down into several lines. However, if $bundle_entity_type_id is 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 valid EntityTypeInterface object to work with.

2.

About the destination stuff, you could also try this:

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::testFieldAdminHandler still tests green locally, while failing with the testbot. I guess this shouldn't be the case and needs further investigation.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

I 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mErilainen made their first commit to this issue’s fork.

merilainen’s picture

Status: Needs work » Needs review
StatusFileSize
new68.5 KB

This changes the message to be an error displayed on the form element. See the attachment how it looks like.

tomimikola’s picture

Status: Needs review » Reviewed & tested by the community

Nice 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.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

The MR is failing tests and not mergeable. Thanks!

xjm’s picture

I 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!

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.