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

Command icon 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

rodrigoaguilera created an issue. See original summary.

rodrigoaguilera’s picture

Status: Active » Needs review
daften’s picture

We 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.

jurgenhaas’s picture

Turns 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.

daften’s picture

In 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:

if ($entity_type->hasKey('bundle')) {
    if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
      $fields[...] = BaseFieldDefinition::create('entity_reference')
    }
    else {
      $fields[...] = BaseFieldDefinition::create('string')
    }
  }

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.

xmacinfo’s picture

Priority: Normal » Major

Coming 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.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community
Related issues: +#2987537: Custom menu link entity type should not declare "bundle" entity key

So, 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.

joachim’s picture

Status: Reviewed & tested by the community » Needs work

> 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.

jurgenhaas’s picture

Status: Needs work » Needs review

Thank 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.

f0ns’s picture

I 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.

f0ns’s picture

Status: Needs review » Reviewed & tested by the community

mherchel made their first commit to this issue’s fork.

mherchel’s picture

Merged 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.

  • mherchel committed b3a039ee on main
    fix: #3610934 Fatal error on edit forms of entities without bundle...
mherchel’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed b3a039eeace to main. Thanks!

  • mherchel committed f4f2a15f on 11.x
    fix: #3610934 Fatal error on edit forms of entities without bundle...

  • mherchel committed 4bfc0b1d on 11.4.x
    fix: #3610934 Fatal error on edit forms of entities without bundle...
mherchel’s picture

Status: Patch (to be ported) » Fixed

Backported 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!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

xmacinfo’s picture

Will there be a follow up to fix the issue upstream in Core or is this a fix only for Default Admin?

jurgenhaas’s picture