Problem/Motivation

If you have no bundle for a entity type created in your Drupal instance, you can't add or edit a view mode.

TypeError: array_keys(): Argument #1 ($array) must be of type array, null given in array_keys() (line 119 of /core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php).

The EntityDisplayModeFormBase class expects at minimum one bundle per entity type.

This behavior was introduced by #3359240: Enable bundle selection when a new view mode is created

Steps to reproduce

  1. Install Drupal
  2. Delete all node bundles if present
  3. Try to add a new view mode for content
  4. Observe the watchdog log for the given error message

Proposed resolution

Add a defense, so the form could be rendered without a bundle.
Add a test.

Remaining tasks

User interface changes

Maybe we could hide the bundle selection form element in case no bundle is present.

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3593466

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

sunlix created an issue. See original summary.

sunlix’s picture

Status: Active » Needs review

I've added an easys defense to prevent the add/edit view mode dialog from not rendering.
In behalf of the UI maybe some maintainer have to commit for hiding the bundles field on the dialog.
If so, we can iterate over this issue, but for now it works as expected and the test proof this.

dcam’s picture

Title: No view mode can't be added if not entity bundle exists » View mode form crashes if there are no bundles
Priority: Normal » Major
Status: Needs review » Needs work

Thank you for reporting this issue and for providing a merge request!

I am increasing the priority to Major, both because it qualifies as a Major bug and because it's now more likely that people will encounter this bug since Core doesn't ship with content types by default anymore.

I've reviewed the MR. The fix is simple enough and I don't have any feedback about it. But while I greatly appreciate that you included a test for this bug, it is a FunctionalJavascript test and that needs some work. FunctionalJavascript tests are expensive and slow to run. The things being done in the test are simple enough (or can be) that there's no reason for it. This could easily be a Functional test, probably even Kernel test which is much cheaper to run. The caveat is that you need to access the form directly, /admin/structure/display-modes/$display_mode/add/node, rather than clicking through to it via the main view mode list. You'll also need to assert for different text on the page because the title doesn't appear.

sunlix’s picture

Status: Needs work » Needs review

Thanks for your input.
I have moved the test to a kernel test for lower footprint and faster runtime.

The test currently covers building the view mode form.
We can extend that for form mode form, too. But I think view mode form is enough, due to the fact that both shares the logic via EntityDisplayModeFormBase::form().

dcam’s picture

Status: Needs review » Reviewed & tested by the community

This looks great to me. Thank you for rewriting that test.

catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to main, 11.x and 11.4.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • catch committed 11c55fb3 on 11.4.x
    fix: #3593466 View mode form crashes if there are no bundles
    
    By: sunlix...

  • catch committed 8bd82309 on 11.x
    fix: #3593466 View mode form crashes if there are no bundles
    
    By: sunlix...

  • catch committed 4f0c2dc6 on main
    fix: #3593466 View mode form crashes if there are no bundles
    
    By: sunlix...