Problem/Motivation
With the menu_item_fields module installed visiting the edit form of a menu_link_content entity results in a fatal error:
Error: Call to a member function label() on null in Drupal\default_admin\Hook\PreprocessHooks->preprocessBreadcrumb() (line 192 of core/themes/default_admin/src/Hook/PreprocessHooks.php).
It is due to the following code
$bundle_key = $entity_type->getKey('bundle');
if ($bundle_key) {
$bundle_entity = $entity->get($bundle_key)->entity;
$type_label = $bundle_entity->label();
}
Menu link content entities are weird in the sense that they have a bundle key but no configuration entity. The above code assumes there is always an entity backing the bundle with a label.
Steps to reproduce
Install menu_item_fields and edit a menu_link_content entity
Proposed resolution
Modify code to check for the bundle entity before accessing its label
Remaining tasks
Code
User interface changes
None
Issue fork drupal-3610934
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 #3
rodrigoaguileraComment #4
daften commentedWe ran into the same problem and create the patch from the MR verbatim, after which we dug around to find this. It works 100%.
Leaving as in review since I think another person reviewing is necessary since there's no test coverage.
Comment #5
jurgenhaasTurns out, there is a duplicate issue at #3613636: MenuLinkContent should not define the bundle entity key and we discussed there, that the root cause is that the MenuLinkContent entity has that bundle key which should be removed to fix the issue. Changing this in default_admin would just deal with the symptom, but doesn't fix the real issue.
Comment #6
daften commentedIn the meantime I had already created tests, and updated the MR :)
I agree the root cause should be fixed by ensuring everything is in order with menu_link_content, however I also think this can still be an issue to be fixed:
In ContentEntityBase::baseFieldDefinitions() this piece of code is there:
And the return for EntityTypeInterface::getBundleEntityType() is documented as the name of the entity type which provides bundles, or NULL if the entity type does not have a bundle entity type.
So having preprocessBreadcrumb reading ->entity and then calling -> label can lead to this bug for other entity types without bundle entity type, and core seems to support that. So the root cause is deeper than just MenuLinkContent doing this.
What do you think? Am I wrong in my analysis and overlooking something? :)
Leaving this in needs review so the tests can be reviewed properly.
Comment #7
xmacinfoComing from: #3614584: default_admin fatal error on custom menu link translation page
I consider this major since I had to disable Default Admin theme and swith back to Claro.
Comment #8
jurgenhaasSo, while this MR just fixes the symptom and not the root cause, it's still the right thing to add this as a safeguard, as other "mis-configured" entity types could cause similar issues in the future, even after #2987537: Custom menu link entity type should not declare "bundle" entity key may have been fixed.
I've tested the MR and it does what it says. This should hopefully go with the next patch release of core.
Comment #9
joachim commented> if ($bundle_key && $bundle_entity = $entity->get($bundle_key)->entity) {
That is not a particularly clean way to check for this. For code-defined bundles, the bundle field is not an entity reference field, so you're trying to access a property that might not be there.
The goal here is to get a bundle label, in all cases, and looking for an entity is not the right way.
The correct way to get a bundle label is with the bundle info service.
Comment #10
jurgenhaasThank you @joachim, good catch. I've updated the MR accordingly. The 2 failing tests are unrelated and just need a retry that I can't trigger but asked for on Slack.
Comment #11
f0ns commentedI had the same issue.
An error on edit of every menu link item in my site.
I tested the MR and this fixed the issue for me.
I also reviewed the code and this looks like a sensible fix, would love to see this one get released soon.
Comment #12
f0ns commentedComment #14
mherchelMerged in main, and now tests are passing. I discussed this with @jurgenhaas earlier, and he walked me through the code. I also installed menu_item_fields to generate the error and verified that this fixes it.
Comment #16
mherchelCommitted and pushed b3a039eeace to main. Thanks!
Comment #19
mherchelBackported to 11.x and 11.4.x, since this is a real-world issue and we need folks to test 11.4 as much as possible.
Thank you everyone!
Comment #21
xmacinfoWill there be a follow up to fix the issue upstream in Core or is this a fix only for Default Admin?
Comment #22
jurgenhaas@xmacinfo see 8 year old related issue #2987537: Custom menu link entity type should not declare "bundle" entity key