In the "MenuBasedBreadcrumbBuilder" class we inject some dependencies and the one called "menu.active_trail" particularly.
This core service is constructed using the current request route match object.
But when comes the time to render breadcrumbs with the methods "applies" and "build" which have a route match object as a parameter.
This object can differ from the one used by already created services dependencies, and lead to unexpected results.
To reproduce this issue, you might want to use the following code :
$entity = Node::load(1);
$routeName = $entity->toUrl()->getRouteName();
$routeParameters = $entity->toUrl()->getRouteParameters();
$route = \Drupal::service('router.route_provider')->getRouteByName($routeName);
$routeMatch = new RouteMatch($routeName, $route, $routeParameters, $routeParameters);
$breadcrumbs = \Drupal::service('breadcrumb')->build($routeMatch)->toRenderable();
The breadcrumbs generated here could differ from the one you would see on the node view page itself. This is because the menu active trails is not the same when viewing the node and when viewing any other page.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | interdiff.txt | 3.53 KB | renrhaf |
| #6 | use-given-routematch-3026188-6.patch | 4.29 KB | renrhaf |
| #2 | use-given-routematch-3026188-2.patch | 2.31 KB | renrhaf |
Comments
Comment #2
renrhafHere is the kind of patch that could be used to fix this issue.
It must be improved with dependency injection though.
I just created it quickly to have some insight of other developers that might have faced the issue too.
Comment #3
renrhafComment #4
renrhafComment #5
rphair commentedThis looks reasonable to me. I understand the patch will be complete when we can use dependency injection for the 3 services that are used to build the MenuActiveTrail object without using the injected @menu.active_trail service (which has been removed).
Unfortunately I am travelling with crappy broadband right now, with unreliable SSH access: I'd probably be able to push this out into dev although editing & testing would be a problem. If @Renrhaf or anyone else can upload a complete patch then I'll try to push it out, otherwise will make the mods myself as soon as I get proper Internet again.
Comment #6
renrhafHi rphair, here is the complete patch you asked ;)
Comment #7
renrhafComment #8
rphair commentedthanks @Renrhaf - Internet is still appalling where I am & I wouldn't be able to fix things if something went wrong with the release... please trust that I will check this into dev as soon as I am able.
Comment #10
rphair commentedthanks again @Renrhaf - it looks like this will improve the general usability of this module so I'll release this in version 1.8 if no problem reports in next 2 weeks (scheduled for 08 February or ASAP afterwards).
Comment #11
renrhafPerfect, thank you for the great module !
Comment #13
rphair commentedTo all those who have been following this issue: we have a request to undo this change in favour of the original design, particularly because it breaks compatibility with the "drupal/menu_position" module.
Please, @Renrhaf and any others following or identifying with this issue: we need your input about #3036501: Service menu.active_trail dependency injection to determine by reasoning and consensus what the correct behaviour should be, including whether an option might be added to support both an injected and a derived MenuActiveTrail.