Incorrect active menu item, when same path is added to menu as child and parent

Created on 6 May 2016, about 9 years ago
Updated 25 July 2025, 9 days ago

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);
      }
πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

menu system

Created by

πŸ‡ͺπŸ‡ͺEstonia Martin.

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for reporting this problem. We rely on issue reports like this one to resolve bugs and improve Drupal core.

    Since there has been no activity here for over 8 years we are asking if this problem persists on a currently supported version of Drupal. To help, add a comment explaining if the problem still occurs or not. Any extra detail you can provide can help others who experienced this.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
    Thanks!

Production build 0.71.5 2024