When building active trail, Drupal assumes that the first link matching the current path is the active link.
It is not always true, especially when using the Menu Firstchild module (see http://drupal.org/node/671564 about this problem).

Consider you have this kind of menu:

item A linking to path 1
-- item B linking to path 1
---- item C linking to path 1
---- item D linking to path 2
-- item E linking to path 3

Clicking on item A, item B or item C will bring user to the same path 1.
As item A and item B are just parent containers of item C, item C should be the active link.
But Drupal shows item A as the active link, ignoring item B and item C.

To fix this bug, loop should continue until the last descendant matching current path is found:

<?php
    while ($curr) {
      // Terminate the loop when we find the current path in the active trail.
      // patch
      /*
      if ($curr['link']['href'] == $item['href']) {
        $trail[] = $curr['link'];
        $curr = FALSE;
      }
      else {
      */
        // Add the link if it's in the active trail, then move to the link below.
        if ($curr['link']['in_active_trail']) {
          $trail[] = $curr['link'];
          $tree = $curr['below'] ? $curr['below'] : array();
        }
        list($key, $curr) = each($tree);
      /*
      }
      */
      //~patch
    }
?>

Comments

kroimon’s picture

+1 for this one as it really fixes the issue and doesn't seem to create new ones

anrikun’s picture

Assigned: Unassigned » anrikun
Status: Active » Needs review
StatusFileSize
new1.1 KB

Here's the corresponding patch:

Status: Needs review » Needs work

The last submitted patch, menu.inc_.patch, failed testing.

anrikun’s picture

Status: Needs work » Needs review

#2: menu.inc_.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, menu.inc_.patch, failed testing.

anrikun’s picture

Could someone tell me what's wrong with the format of this patch?

damien tournoud’s picture

Version: 6.16 » 7.x-dev

See http://drupal.org/patch/create for more information about how to create a patch for Drupal.

I would not call this a bug per say, it's just a difference between what you expect the menu system to do and what it actually does. Anyway, bugs are fixed and discussed first in the current development version (7.x) before being eventually backported.

anrikun’s picture

I've followed http://drupal.org/patch/create throughoutly to create the patch.
That's why I don't get why it doesn't pass the test :-(

anrikun’s picture

StatusFileSize
new1.07 KB

Updated patch:

drifter’s picture

Status: Needs work » Needs review

Set the issue to the "needs review" state if you want it tested. It will fail again though, you need to create it from the drupal site root, not the menu module root.

Status: Needs review » Needs work

The last submitted patch, menu.inc__24.patch, failed testing.

anrikun’s picture

Title: menu.inc/menu_set_active_trail (line 1561): should not terminate the loop when we find the current path in the active trail » menu.inc: active trail and breadcrumb are broken when a parent menu item and a child menu item link to the same path.
Status: Needs work » Needs review
StatusFileSize
new596 bytes

Here's an updated patch for 7.x.
Please consider committing it.
It does not hurt at all and will please all the many users who create parent menu items simply as containers.

I will post a backported 6.x-16 version after this one.

Status: Needs review » Needs work

The last submitted patch, menu.inc_.patch, failed testing.

damien tournoud’s picture

Title: menu.inc: active trail and breadcrumb are broken when a parent menu item and a child menu item link to the same path. » Go as deep as possible when building the active trail
Category: bug » feature

This is actually a feature request. I doubt it's backportable to D6, has it is an important change of behavior.

This needs test, I'm not completely convinced that your patch works as you intend it to.

anrikun’s picture

StatusFileSize
new596 bytes

I've been using this patch on several sites for more than 1 year now and it works perfectly without any side effect.

anrikun’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, menu.inc_.patch, failed testing.

anrikun’s picture

Status: Needs work » Needs review
StatusFileSize
new596 bytes

Status: Needs review » Needs work

The last submitted patch, menu.inc_.patch, failed testing.

anrikun’s picture

StatusFileSize
new596 bytes
anrikun’s picture

StatusFileSize
new588 bytes

Drupal 7.x-dev version

anrikun’s picture

Status: Needs work » Needs review
StatusFileSize
new619 bytes

Drupal 6.16 version

Here is the same patch rolled back for Drupal 6.16:

anrikun’s picture

StatusFileSize
new619 bytes

Drupal 6.19 version

Here is the same patch rolled back for Drupal 6.19:

halefx’s picture

Thank you

aanjaneyam’s picture

Could this patch be committed to drupal core.

anrikun’s picture

StatusFileSize
new556 bytes

Drupal 6.20 version

Here is the same patch for Drupal 6.20:

cyberwolf’s picture

subscribing

HerrSerker’s picture

I want this too in the drupal 6 core. It worked for me and fixed the issue with pathauto and menu firstchild modules
--edit
I changed my opinion.
Does not fix the [menupath-raw] token

I have a structure like

A
B
+-B a
  +-B a 1
  +-B a 2

But automatic url aliases are

A
B
  B
    B
    B/a/2

but should be

A
B/a/1
  B/a/1
    B/a/1
    B/a/2
bensnyder’s picture

subscribe - please commit :)

bryancasler’s picture

+1 for D7 commit

anrikun’s picture

Status: Needs review » Reviewed & tested by the community

Seeing #24, #25, #29, #30, I guess this can changed to RTBC.

catch’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7
damien tournoud’s picture

Status: Reviewed & tested by the community » Needs work

This patch doesn't even apply, and just cannot be right.

It feels like we should add a ORDER BY weight to the query in menu_link_get_preferred(). In the current D7 code, we just pick a link as random as the preferred link if there are several links with the same path in the same menu.

anrikun’s picture

This patch doesn't even apply

You mean on D8?
Do patches have to be for D8 now that is D7 is out?

damien tournoud’s picture

@anrikun: it applies either on D7 nor D8. A lot of things have changed in this area since one year ago :)

anrikun’s picture

Code in D7 has changed a lot indeed!
If I provide a new patch, does it necessarily have to be for D8 first?

anrikun’s picture

FYI, the patch at #26 applies to Drupal 6.22 too.

gagarine’s picture

Status: Needs work » Needs review
Issue tags: -Needs backport to D7

#21: includes_menu_inc.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Needs backport to D7

The last submitted patch, includes_menu_inc.patch, failed testing.

omercioglu’s picture

sub

candelas’s picture

subscribe

adamdicarlo’s picture

Subscribing.

manObject’s picture

Status: Needs work » Needs review

#2: menu.inc_.patch queued for re-testing.

manObject’s picture

This patch fixes a bug in the core Menu module. It has been widely tested by many satisfied users for a long period of time. The code is fine, it doesn't affect compatibility in either direction, it makes Drupal better, so why hasn't it been committed to the core yet?

manObject’s picture

There is a coding error in the patch. The new "if" clause does not have a closing curly bracket. This would have shown up had it been properly indented.

bleen’s picture

Status: Needs review » Needs work

re #36:

If I provide a new patch, does it necessarily have to be for D8 first?

Yup

klonos’s picture

Title: Go as deep as possible when building the active trail » Go as deep as possible when building the active trail.
Assigned: anrikun » Unassigned

...unassigning Henri since he seems to not be working on this.

Do have an agreement on this being a feature request and not a bug? If this is deemed a bug and it doesn't break any API or cause any critical issues, it could be backported to D7 and perhaps D6.

sylvain lecoy’s picture

Full support for this to land in Drupal 7.

klonos’s picture

I consider this to be a bug in my use case. If it remains a simple feature request, I doubt it has any chance of getting in D8 (when in fact this bug goes as back as D6).

dariogcode’s picture

I also need this feature!. Breadcrumbs doesn't work well for first items. Thanks!

klonos’s picture

Category: Feature request » Bug report

...I think I'm gonna go ahead and set this to be a bug after all. Even the issue summary makes it sound like one. I honestly cannot understand under what context people consider this a feature.

steveoriol’s picture

At the moment, anyone know how to operate the Breadcrumb in D7 with the "FirstChild Menu" module?
In fact, it is working with "menu breadcrumb" module but not with the "path breadcrumbs" module...

abarpetia’s picture

@steveoriol: Did you found solution for "FirstChild Menu" module?
Thanks

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.

Syndz’s picture

I ran into the same problem for Drupal 8.

Here's a patch that returns the last found link instead of the first one.

Syndz’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 56: drupal-deepest_active_trail_link-732370-56-d8.patch, failed testing.

Syndz’s picture

Status: Needs work » Needs review
StatusFileSize
new577 bytes

Rerolled patch using git diff

Status: Needs review » Needs work

The last submitted patch, 59: drupal-deepest_active_trail_link-732370-59-d8.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.

bramdriesen’s picture

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

Also bumped into this issue today. Patch seems to fix the issue for me as well.

bramdriesen’s picture

FYI: We are not using the menu block module :) but it's indeed related to this one I guess.

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

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

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.

juanolalla’s picture

Version: 8.9.x-dev » 9.2.x-dev
Status: Needs work » Reviewed & tested by the community

Patch #59 works as expected in 9.2.x

sam152’s picture

Status: Reviewed & tested by the community » Needs work

This is a behavioural change that would require some degree of configuration or backwards compatibility. It also is missing test coverage.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

douggreen’s picture

There's already a code comment in MenuActiveTrail.php that says

    // Note: this is a very simple implementation. If you need more control
    // over the return value, such as matching a prioritized list of menu names,
    // you should substitute your own implementation for the 'menu.active_trail'
    // service in the container.
    // The menu links coming from the storage are already sorted by depth,
    // weight and ID.

So I think the first pass solution is a contrib module that implements this. Before I write such a module, I'm wondering if one exists already.

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.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.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

juanolalla’s picture

I just released a module which just extends the MenuActiveTrail service to do exactly this: https://www.drupal.org/project/menu_active_trail_deepest

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.