Problem
In some cases, there is a notice generated in the build() method of the MenuBasedBreadcrumbBuilder class:
Notice: Undefined index: entity_id in Drupal\menu_breadcrumb\MenuBasedBreadcrumbBuilder->build() (line 253 of modules/contrib/menu_breadcrumb/src/MenuBasedBreadcrumbBuilder.php).
For example: when creating a view and adding it to the menu (not by creating a menu item from the backend, but by setting that view to appear in the menu from the view page settings).
Propose solution
This can be fixed quite easy by making sure that
$def['metadata']['entity_id']
is not empty in the build method.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | undefined_index_entity_id_2831084_2.patch | 845 bytes | vasi1186 |
Comments
Comment #2
vasi1186 commentedAttached a patch that should fix the issue.
Comment #3
vasi1186 commentedComment #4
rphair commentedOK, and we also know that this bug would only manifest in a multi-lingual site. In hindsight I was too hasty in adopting a patch that used EntityTypeManager to turn a menu link plugin ID into a menu link entity, so it had a full entity to translate.
But the problem with your patch is that it won't translate the breadcrumb if the view lacks that metadata as you describe, since it will simply skip the part of the code that translates the menu title. I'm preparing a revised method now; stay tuned...
Comment #5
rphair commentedActually the two methods I tried didn't work:
$entity = $this->menuLinkManager->getInstance(['id' => $id]);but that only generates an object typeDrupal\menu_link_content\Plugin\Menu\MenuLinkContentwhich is not translatable.entityQuerywill look upMenuLinkContentby UUID but not by plugin ID, and would be different for views & regular menu links: possible with string manipulation, but with lots of code that would be non-portable & error-prone.I'll look in similar modules next to see what they are doing: maybe this is only possible by manipulating the menu tree. In the meantime, if anyone knows how to simply fill in the following code to return a translatable entity for all plugin IDs in the menu active trail, please post here:
Comment #6
vasi1186 commentedOK, I see your point, so we have a bit of a bigger issue than just not showing that warning (because even without my patch, if the views does not contain that metadata, the code will not be executed because it will not pass the next if condition).
Will try to think of a solution when I have some time.
Comment #7
rphair commentedjust going through background material and found change record: Menu links converted to plugins, saying "Link title is the (translatable) entity label". That is something to go on, since full entity wouldn't have to be loaded to simply translate the menu title. Just need to find how to work that into a solution; stay tuned...
Comment #8
rphair commentedDue to caching issues we are going to have obtain the menu link entity for all components of the menu trail anyway, so scratch idea #7. This issue is now paired with issue 2827653; when we have an entity for each item on the menu trail it should be both translatable and usable as a cacheable dependency.
Comment #9
rphair commentedComment #10
rphair commentedThe offending code has now gone away in the course of fixing issue 2827653. Others & I have checked that the menu plugins we use now are translatable; please post in one of the relevant issues if you find otherwise.