Problem/Motivation

When adding a new Media reference field in Field UI via the "Add field" modal and attempting to set a default value via Media Library, MediaLibraryFieldWidgetOpener::checkAccess() fails because it tries to access a field that doesn't exist yet on the entity.

The only workaround is to first create a Media reference field without setting the default value, then right-click the "Edit" button to open the edit form in a new tab where the Media Library modal is stored correctly (instead of the new default behavior which opens in a modal).

This is a separate core bug from the nested modal issue #2741877: Nested modals don't work: opening a modal from a modal closes the original which occurs in a similar scenario when setting the default media from the "Edit field" modal. The MediaLibraryFieldWidgetOpener assumes the field already exists on the entity, which isn't true during initial field creation.

Steps to reproduce

  • Add a new Media reference field (field_test_default_media) to a node type.
  • During field creation, try to set a default value via Media Library, either via creating a new item, or selecting an existing item from the Media Library modal popup.
  • Ajax Error: "Field field_test_default_media is unknown" in ContentEntityBase->getTranslatedField()

Analysis from the Ajax Error stack trace:

  • Error location: MediaLibraryFieldWidgetOpener::checkAccess() at line 210 (Drupal 11.3.2)
  • Root cause: It calls $entity->get('field_test_default_media') to check field access
  • Problem: The field doesn't exist yet because it's being created (not edited)
  • Context: The URL shows field_widget_id=field_test_default_media:default_value_input, indicating this is during default value configuration in the "Add Field" workflow.

Proposed resolution

TBD

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

jwilson3 created an issue. See original summary.

jwilson3’s picture

mortona2k’s picture

Ran into this yesterday.

jwilson3’s picture

I hit this issue (and #2741877) yesterday while testing a feature request for SVG Image Field #2987590: Support default image fallback for SVG image fields

quietone’s picture

Version: 11.3.x-dev » main

Issues are fixed on 'main' first.