Hi,

Thanks as always to all helping to maintain the module (:

Disclaimer: I don't know it well and got a little confused trying to follow through at times, so I could easily be wrong...

Problem/Motivation

  1. Install the standard profile: you should have a Home link by default on the Main navigation menu.
  2. Add a field to the main menu bundle. (I don't know why this is necessary.)
  3. Add a (menu_link_content) link to the main menu.
  4. In hook_preprocess_menu_link_content() run $variables['menu_link_content']->getUrlObject();

The following error is generated when preprocessing the Home link:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getUrl() on null in Drupal\menu_link_content\Entity\MenuLinkContent->getUrlObject() (line 88 of core/modules/menu_link_content/src/Entity/MenuLinkContent.php).

If I understand correctly, currently in \Drupal\menu_item_extras\Service\MenuLinkTreeHandler::getMenuLinkItemEntity() it returns a menu_link_content entity regardless of the plugin type. I think it's connected with this PR and issue GH#98. In particular it looks like...

      $menu_item = $this->entityTypeManager
        ->getStorage('menu_link_content')
        ->create($link->getPluginDefinition());

...doesn't pass a value for the link base field when used with MenuLinkDefault.

Proposed resolution

I was wondering if it really makes sense to create a menu link content entity for other types of link (fwiw it came as a surprise to me when I found out), but I don't know the rationale behind that decision. So I was wondering if it might make sense to either

  1. not create a menu_link_content entity for all links;
  2. use \Drupal\Core\Menu\MenuLinkInterface to initialise the content entity from the menu plugin.

Please let me know if I can provide any more debug info, and I'd be happy to help out with a solution.

Thanks!

Comments

AndyF created an issue. See original summary.

andyf’s picture

Issue summary: View changes
kevin.brocatus’s picture

I have built a fallback in the MenuItemExtrasMenuLinkContent entity extension class to generate an Url object based on the object's route_name property.

gantal’s picture

The patch above in #3 removed the error for me. Thank you!

dmitriy.trt’s picture

Priority: Normal » Critical
Status: Active » Needs review
StatusFileSize
new655 bytes

Raising priority to critical as this issue may break whole website in case such a link appears in your main menu. You could easily get such a non-entity link by configuring a view with a page display having a menu item. It may be the missing piece in the steps that allows you to reproduce the issue.

It's still reproducible on version 8.x-2.15.

The attached patch removes creation of the menu link entity for non-entity links.

ozin’s picture

Assigned: Unassigned » ozin

I will check the patch as soon as possible, thanks!

  • ozin committed a13c4b3 on 8.x-2.x authored by kevin.brocatus
    Issue #3032524 by kevin.brocatus: getUrl() fatals for menu_link_content...
ozin’s picture

Status: Needs review » Fixed
dmitriy.trt’s picture

@ozin Could you please explain why it is necessary to create a menu link entity on-the-fly for non-entity links? Because it's really not obvious, none of the tests seem to cover this case.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.