Warning: Undefined array key "view_mode" while creating the field

Created on 13 February 2024, 10 months ago

Problem/Motivation

The following warning is shown when the View Mode Selector field is created for the entity type for the first time:

Warning: Undefined array key "view_mode" in Drupal\Core\Entity\EntityDisplayRepository->getAllDisplayModesByEntityType() (line 124 of core/lib/Drupal/Core/Entity/EntityDisplayRepository.php).

The same happens if the 'View Mode Selector' entity display is removed.

This is happening because view_mode_selector_entity_view_mode_info_alter is called from \Drupal\Core\Entity\EntityDisplayRepository::getAllDisplayModesByEntityType which populates $this->displayModeInfo[$display_type] and then calls alter hooks. However, the view_mode_selector_entity_view_mode_info_alter method creates a new display mode which in turn calls \Drupal\Core\Entity\EntityDisplayRepository::clearDisplayModeInfo and clears previously populated $this->displayModeInfo array.

Steps to reproduce

Create the first View Mode Selector field for some entity type.
Alternatively, remove the View Mode Selector display mode for any existing entity type and refresh the page.
Observe the warning.

Proposed resolution

Do not create a display mode config entity in hook_entity_view_mode_info_alter implementation; probably some field config entity insert hook would be a better place to do this.

User interface changes

None.

API changes

None.

Data model changes

None.

๐Ÿ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ฆUkraine abramm Lutsk

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024