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.

CommentFileSizeAuthor
#2 undefined_index_entity_id_2831084_2.patch845 bytesvasi1186
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vasi1186 created an issue. See original summary.

vasi1186’s picture

Attached a patch that should fix the issue.

vasi1186’s picture

Status: Active » Needs review
rphair’s picture

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

rphair’s picture

Status: Needs review » Needs work

Actually the two methods I tried didn't work:

  1. The alpha version of the module used $entity = $this->menuLinkManager->getInstance(['id' => $id]); but that only generates an object type Drupal\menu_link_content\Plugin\Menu\MenuLinkContent which is not translatable.
  2. entityQuery will look up MenuLinkContent by 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:

foreach (array_reverse($this->menuTrail) as $id) {
  $entity = ???;
}
vasi1186’s picture

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

rphair’s picture

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

rphair’s picture

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

rphair’s picture

Assigned: Unassigned » rphair
rphair’s picture

Status: Needs work » Fixed

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

Status: Fixed » Closed (fixed)

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