EntityHelper::getEntityBundle can sometimes return NULL despite typing

Created on 2 January 2025, 22 days ago

Problem/Motivation

I've found a case where EntityHelper::getEntityBundle sometimes attempts to return a NULL even though it's typed to return a string. I believe the Menu Item Extras module can set menu_link_content entities into a state where `$entity->getMenuName()` can return a NULL even though we obviously don't want it to do so.

In any case, we should ensure that EntityHelper::getEntityBundle returns a string in all cases, even if it means treating an entity like a bundle-less entity. I believe this issue 🐛 option argument 'bundle' sometimes has a NULL value, which can cause issue Fixed covers a similar state, and they proposed to solve it by falling back to the entity type ID.

Steps to reproduce

  1. Set up a Drupal site with Menu Item Extras
  2. Add Simple Sitemap
  3. Enable Menu Item Extras on the main menu
  4. Try indexing main menu items for the sitemap

Proposed resolution

Apply fallbacks following the following pattern.

  1. If entity type ID is menu_link_content, and entity has method `getMenuName`, try calling `$entity->getMenuName()` but fall back to `$entity->bundle()`
  2. Otherwise call `$entity->bundle()`
  3. If bundle still appears to be NULL, fall back to ``$entity->getEntityTypeId()`

Remaining tasks

Implement patch

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

4.2

Component

Code

Created by

🇦🇺Australia geoffreyr

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024