Undefined offset: 1 in Drupal\Core\Entity\EntityDisplayRepository->getAllDisplayModesByEntityType()

Created on 3 February 2021, almost 4 years ago
Updated 15 January 2024, 11 months ago

Problem/Motivation

I'm getting the following notice:

Notice: Undefined offset: 1 in Drupal\Core\Entity\EntityDisplayRepository->getAllDisplayModesByEntityType() (line 115 of core/lib/Drupal/Core/Entity/EntityDisplayRepository.php).
Drupal\Core\Entity\EntityDisplayRepository->getAllDisplayModesByEntityType('view_mode') (Line: 142)
Drupal\Core\Entity\EntityDisplayRepository->getDisplayModesByEntityType('view_mode', 'block_content') (Line: 78)
Drupal\Core\Entity\EntityDisplayRepository->getViewModes('block_content') (Line: 165)
Drupal\field_ui\Plugin\Derivative\FieldUiLocalTask->getDerivativeDefinitions(Array) (Line: 101)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 284)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 175)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 201)
Drupal\Core\Menu\LocalTaskManager->getDefinitions() (Line: 226)
Drupal\Core\Menu\LocalTaskManager->getLocalTasksForRoute('entity.menu.collection') (Line: 310)
Drupal\Core\Menu\LocalTaskManager->getTasksBuild('entity.menu.collection', Object) (Line: 378)
Drupal\Core\Menu\LocalTaskManager->getLocalTasks('entity.menu.collection', 0) (Line: 95)
Drupal\Core\Menu\Plugin\Block\LocalTasksBlock->build() (Line: 171)
Drupal\block\BlockViewBuilder::preRender(Array)

This error seems to be proceeding from Drupal\Core\Entity\EntityDisplayRepository->getAllDisplayModesByEntityType(). Line 115
113 $this->displayModeInfo[$display_type] = [];
114 foreach ($this->entityTypeManager->getStorage($entity_type_id)->loadMultiple() as $display_mode) {
115 list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode->id(), 2);
116 $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode->toArray();
117 }
118 $this->moduleHandler->alter($key, $this->displayModeInfo[$display_type]);
119 $this->cacheSet("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, ['entity_types', 'entity_field_info']);

I attach an image.

Steps to reproduce

Every time I update the cache in my environment, I'm getting this notice in each page that I access.

Proposed resolution

Here I found a possible solution and at first sight it seems to work, I'm not getting the error anymore: https://www.daniweb.com/programming/web-development/threads/452877/how-t...

πŸ› Bug report
Status

Needs work

Version

9.5

Component
EntityΒ  β†’

Last updated about 2 hours ago

Created by

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States jfurnas

    I can confirm that today, running Drupal Core 9.5.9 that I start seeing this issue immediately after a cache clear whenever I view a page that loads or uses entities. This is the error I am receiving as of today:

    Warning: Undefined array key 1 in Drupal\Core\Entity\EntityDisplayRepository->getAllDisplayModesByEntityType()

  • πŸ‡ΊπŸ‡ΈUnited States capysara

    What php version?

  • πŸ‡«πŸ‡·France GuillaumePacilly

    For those still struggling with this:

    The issue is raised by the method \Drupal\Core\Entity\EntityDisplayRepository::getAllDisplayModesByEntityType on line 115:
    [$display_mode_entity_type, $display_mode_name] = explode('.', $display_mode->id(), 2);

    PHP8 will throws a Notice: Undefined offset 1 if explode('.', $display_mode->id(), 2) returns an array with only on argument.
    This will be the case if you have a display mode like this in your config: core.%display_mode_entity_type%.%node_type%. It should be core.%display_type%.%display_mode_entity_type%.%display_mode_name%

    In my case I had an obsolete config like core.%display_mode_entity_type%.%node_type%. Removing it with drush solved the issue
    drush config:delete core.%display_mode_entity_type%.%node_type% && drush cex -y

Production build 0.71.5 2024