When multiple menu items point to the same page, the active trail is often not the intended one. This happens a lot. It is very difficult that a breadcrumb and a URL are singular but a menu item can be multiple.

The trouble comes in menu_link_get_preferred. Various solutions have been presented such as preferring the item that is most deeply nested #306412: Get the deepest menu entry when more than one entry points to the same content or by having multiple active trails #609542: Active trail isn't set on all menu items pointing to the current path.

One simple improvement I think we should make is to, for a node, favor the per-node menu settings over the 'customized' menu items made via menu administration. Because only one menu item can be made on a node form, treating this item as canonical relative to active trail (like we treat its path alias setting there as canonical) makes a lot of sense and is easy for editors to understand.

What we currently have is no real ordering of which menu item to prefer in an active path, so adding a bit of ordering that makes sense to users seems like a big step forward. And also a one line patch. Creds to Timmy P.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, menu-order-customized.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review

menu-order-customized.patch queued for re-testing.

barancekk’s picture

FileSize
535 bytes

I edited the patch to make it work on 7.22 version of drupal. Attaching in this comment.

Thank you

Status: Needs review » Needs work

The last submitted patch, core-1875824-3-d7.patch, failed testing.

stefan.r’s picture

tim.plunkett’s picture

Status: Closed (duplicate) » Needs work

Can you explain how this is a duplicate? That's a 7.x issue.

stefan.r’s picture

Sorry, I had missed that this was a D8 issue with the D7 patch and all, the other issue contained a patch which did essentially the same.

How would this be implemented in D8? "customized" seems to have been replaced by MenuLinkInterface::isResettable(), but that doesn't mean the same thing as "customized" any more.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

khaled.zaidan’s picture

Looking at this issue in D8.

I've found a possible fix attached in the patch (actually 2 potential fixes, hence the 2 patches).

The issue happens when the MenuActiveTrail service decides what the active trail is. This happens in Drupal\Core\Menu\MenuActiveTrail::getActiveLink(). We can see in that function, loadLinksByRoute() returns all the links the match the current route, and then simply the first one of the links is chosen as the active trail.

if ($route_name) {
  $route_parameters = $this->routeMatch->getRawParameters()->all();

  // Load links matching this route.
  $links = $this->menuLinkManager->loadLinksByRoute($route_name, $route_parameters, $menu_name);
  // Select the first matching link.
  if ($links) {
    $found = reset($links);
  }
}
return $found;

So we can either:
1) Instead of simply picking the first link here, we do something smarter to pick the one that appears in the node form (I'll get to how in a below).
Or
2) Make loadLinksByRoute() give preference to the link that appears on the node form.

So I looked at how the link that appears on the node form is chosen. And found that it's chosen in menu_ui_get_menu_link_defaults($node), which sorts links by mlid (or id in the menu_link_content table).

So we can mimic that behaviour by either choosing the link with lowest mlid in Drupal\Core\Menu\MenuActiveTrail::getActiveLink() or by sorting ascendingly by mlid in loadLinksByRoute().

I looked through the code for uses of loadLinksByRoute(), and didn't find any functionality (in core at least) using this other than the active trail. And there doesn't seem to be any reason for the specific order links currently come back in. So I see no harm in changing it, and for the sake of consistency across any potential related functionalities, I think going with this option (option 2) makes more sense.

Anyway, I hope someone could have a look at these patches, and hopefully we see this issue fixed soon.

PS. Attached patches are against 8.2.x-dev.

gmclelland’s picture

Status: Needs work » Needs review

Patched attached. Setting to NR

The last submitted patch, 10: menu-active-trail-consistency-option1-1875824-10.patch, failed testing.

khaled.zaidan’s picture

Oh, I'm feeling silly now, I had a bit of bad code in the patch for option 1 here:

$link_mlid = $link_mlid;

Attached is a modified version.

Status: Needs review » Needs work

The last submitted patch, 13: menu-active-trail-consistency-option1-1875824-13.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.