Problem/Motivation
Problem:
When adding 2 links to menus referring to same position and we have a nesting going on, then wrong active menu link is returned (matter of perspective though)
This problem occurs when the parent and the child are referring to the same path.
Steps to reproduce:
Create a menu with 3 levels.
Add same node to 2nd and 3rd depth.
Add 3 menu blocks, each rendering only one depth
You will see that when you go to your conficting node, then second level menu renders fine, but 3rd level is empty cause drupal thinks active menu link is the one on the 2nd level and not 3rd.
Proposed resolution
Atm drupal finds out the active links based on active route. This happens on MenuActiveTrail.php methid getActiveLink and line 137, where there is links array which is reset. If it finds more than one link it will just take the first one. My suggestion is to to take the last one instead cause it's deeper and is probably the link we are looking for. (This suggestion is based on the assumption that the links array sorted by the occurance depth)
Change this
$links = $this->menuLinkManager->loadLinksByRoute($route_name, $route_parameters, $menu_name);
// Select the first matching link.
if ($links) {
$found = reset($links);
}
to this
$links = $this->menuLinkManager->loadLinksByRoute($route_name, $route_parameters, $menu_name);
// Select the first matching link.
if ($links) {
$found = end($links);
}