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 covers a similar state, and they proposed to solve it by falling back to the entity type ID.
Steps to reproduce
- Set up a Drupal site with Menu Item Extras
- Add Simple Sitemap
- Enable Menu Item Extras on the main menu
- Try indexing main menu items for the sitemap
Proposed resolution
Apply fallbacks following the following pattern.
- If entity type ID is menu_link_content, and entity has method
getMenuName, try calling$entity->getMenuName()but fall back to$entity->bundle() - Otherwise call
$entity->bundle() - 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.
Issue fork simple_sitemap-3496884
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
Comment #2
geoffreyr commentedComment #4
geoffreyr commentedComment #5
geoffreyr commentedComment #6
walkingdexter commentedI can't reproduce the problem by following these steps with Menu Item Extras 3.1.0. Please provide more information.
In this case the problem is more obvious, as the documentation says that
FieldDefinitionInterface::getTargetBundle()can return NULL.Comment #7
walkingdexter commentedClosing due to lack of activity. Feel free to reopen if you can provide more information.
Comment #8
sonnyktI'm having the same error with just core Menu Link Content.
[error] TypeError: Drupal\simple_sitemap\Entity\EntityHelper::getEntityBundle(): Return value must be of type string, null returned in Drupal\simple_sitemap\Entity\EntityHelper->getEntityBundle() (line 127 of /app/web/modules/contrib/simple_sitemap/src/Entity/EntityHelper.php) #0 /app/web/modules/contrib/simple_sitemap/src/Manager/EntityManager.php(522): Drupal\simple_sitemap\Entity\EntityHelper->getEntityBundle()With my debugging, the menu item has empty menu_name:
and
getMenuName()returns NULL for this itemA deep dive into the database tables of this particular item shows a mismatch in language code between the entity and its revision, hence
getMenuName()returns NULL despite the entity has a menu_name in its data table:The cause of the mismatch is unknown, but
$entity->getMenuName()returning NULL causesEntityHelper::getEntityBundle()to return a TypeError. Not only it breaks the sitemap generating in cron, it also breaks the menu item editing UI due to the same type error.As
$entity->getMenuName()does not always return a string, I believe thatEntityHelper::getEntityBundle()needs to check for its return value.Comment #9
anybodyI can also confirm this issue. This entirely blocks deleting a custom menu item, as the error appears and the item can't be deleted. Implementation needs to be more defensive, as multiple people already ran into this.
Comment #10
anybodyMR !118 fixes the issue for me, now the menu item can be deleted as expected. Still the maintainer should check, if the implementation is the correct fix, I can't tell.
Comment #11
gbyteComment #13
gbyteComment #15
anybody@gbyte could you please tag a new release with this fix? Thanks! :)
Comment #16
gbyteWill do soon, though I would add releasing a new minor version is a bit more involved than 'tagging .'
Comment #17
anybodyYes, sorry if my phrasing was incorrect.
Comment #18
gbyteThat's done.