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
- Install the standard profile: you should have a Home link by default on the Main navigation menu.
- Add a field to the main menu bundle. (I don't know why this is necessary.)
- Add a (
menu_link_content) link to the main menu. - 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
- not create a
menu_link_contententity for all links; - use
\Drupal\Core\Menu\MenuLinkInterfaceto 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!
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3032524-dont-create-entity-on-non-entity-links-5.patch | 655 bytes | dmitriy.trt |
| #3 | menu_item_extras-url_object_fatal-3032524.patch | 845 bytes | kevin.brocatus |
Comments
Comment #2
andyf commentedComment #3
kevin.brocatus commentedI have built a fallback in the MenuItemExtrasMenuLinkContent entity extension class to generate an Url object based on the object's route_name property.
Comment #4
gantal commentedThe patch above in #3 removed the error for me. Thank you!
Comment #5
dmitriy.trt commentedRaising 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.
Comment #6
ozinI will check the patch as soon as possible, thanks!
Comment #8
ozinComment #9
dmitriy.trt commented@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.