The function menu_link_get_preferred() returns sometimes the wrong menu item.

In my case, I have a website with i18n and i18n_menu.
I have a view with the path /news. This path is aliased for another language, let's say in french : /actualites.

Then I have a single menu in which I put menu items for each language. I set a language for each menu item.

The problem is that the function menu_link_get_preferred() looks for the router path, not the alias, and doesn't take care of the item language when searching for a menu item candidate.

Would it be possible to add a tag to the query inside menu_link_get_preferred() or add a drupal_alter to filter query results?
Just as suggested here : http://drupal.org/node/881322#comment-4866066

For more information, please see this issue : #881322: Multilevel menu fails to create active menu trail with same link_path in different languages.

Another reason to alter this query is to allow the sorting of duplicate menu items, which allows the active menu trail to use an item other than the last one entered. For example, if, say, node/1 was placed in the same menu in two different places, the second placement would be the active menu trail. With this tag, and an implementation of hook_query_alter() or hook_query_TAG_alter(), the first item can be used instead, allowing the original menu placement to remain in the active trail.

Comments

ludo.r’s picture

Issue summary: View changes

more info

berdir’s picture

Status: Active » Closed (works as designed)

Misread.

berdir’s picture

Status: Closed (works as designed) » Active
EtienneRd’s picture

Version: 7.16 » 7.15
Status: Active » Needs review
StatusFileSize
new591 bytes

There's a tag translatable added many times in Drupal Core, but not used (there's no hook_query_translatable_alter()), that could be added.

Adding it in menu_link_get_preferred() query would allow i18n_menu to alter it and then filter by language the results.

See #881322: Multilevel menu fails to create active menu trail with same link_path in different languages for the upcoming patch to i18n_menu.

EtienneRd’s picture

Title: Add a query tag to the query in menu_link_get_preferred() to allow modules to alter the query » Let other modules handle result of menu_link_get_preferred()
Version: 8.x-dev » 7.15
Issue tags: -Needs backport to D7
StatusFileSize
new1018 bytes

Apparently, using translatable tag, since it's being used in _menu_build_tree() is creating issues when administrating translations with the patch I posted in the i18n_menu issue.

I simply fixed it, since this tag is NEVER user in the Drupal Core, by deleting its addition in _menu_build_tree().

Applying the joined patch will make the patch I posted in this issue work without any failure.

berdir’s picture

Title: Let other modules handle result of menu_link_get_preferred() » Add a query tag to the query in menu_link_get_preferred() to allow modules to alter the query
Version: 7.15 » 8.x-dev
Issue tags: +Needs backport to D7

Wrong fix, you should instead use a different, new tag for the query. translatable has a specific purpose (that is not really used anywhere, but that's a different topic, just removing it is not an option as someone out there might rely on it).

Als updated the title and this will need to be added to 8.x first and then backported.

EtienneRd’s picture

Title: Let other modules handle result of menu_link_get_preferred() » Add a query tag to the query in menu_link_get_preferred() to allow modules to alter the query
Issue tags: +Needs backport to D7
StatusFileSize
new635 bytes

I agree that using an existing tag could cause some issues to users who might be using it.

So I created translatable_preferred_menu_links tag, and I added it to menu_link_get_preferred().

Here's the patch.

I'm posting a patch related to this one in the i18n menu issue.

harrrrrrr’s picture

Status: Needs review » Reviewed & tested by the community

This patch, combined with the one mentioned in the i18n menu issue works when you have a single menu in which you put menu items for each language.

David_Rothstein’s picture

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

As mentioned above, this needs to go into Drupal 8 before it can be considered for backport.

Also, the code comment needs some editing for grammar (or maybe remove the comment altogether? - not sure if it's actually necessary).

czigor’s picture

I have the same problem as in the issue description. I can confirm that #6 works for D7.

czigor’s picture

Issue summary: View changes

fix link

weri’s picture

A solution with a query tag would be great, but this tag should not translation specific as in the patch #6 ("translatable_preferred_menu_links"). More like "preferred_menu_links" in case we have to alter the preferred menu link in other situations, when multiple menu links use the same link path.

Or would it a better solution when this manipulation is possible over a hook like "hook_menu_link_get_preferred_menu_link($menu_links)" after the query?

jeffam’s picture

Version: 8.x-dev » 7.x-dev
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs backport to D7
StatusFileSize
new494 bytes

I, too, needed a way to alter the query in menu_link_get_preferred(), and adding a tag was the perfect way to do it.

In my (client's) case, they were adding the same node twice into the same menu, and the second menu item was always activated when they wanted the first one to be.

I suspect that this is because, lacking any order by clause on the query in that function, rows were returned in the order created and the newest one (i.e. the one with the highest mlid) became the preferred candidate for the given path.

With the tag added, I am able to do a simple:

/**
 * Implements hook_query_TAG_alter().
 */
function example_query_preferred_menu_links_alter(QueryAlterableInterface $query) {
  $query->orderBy('mlid', 'DESC');
}

weri's suggestion to use a more generic tag made sense to me, so that's the one I used.

Lastly, I switched this back to Drupal 7 because the query in menu_link_get_preferred() has been replaced with a call to entity_load_multiple_by_properties() (I guess menu links are entities in D8?) and I have no idea how to alter that result set.

weri’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed and tested the patch #11. It works as expected and I think it's RTBC.

david_garcia’s picture

Patch #11 is good.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

David_Rothstein’s picture

Status: Needs work » Needs review
David_Rothstein’s picture

Status: Needs review » Reviewed & tested by the community

Guess this goes back to RTBC if the test failure was a fluke.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

David_Rothstein’s picture

Status: Needs work » Reviewed & tested by the community
David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

David_Rothstein’s picture

Status: Needs work » Reviewed & tested by the community
jmking’s picture

jmking’s picture

There we go. Patch passed testing now.

I'd really love to see this get committed. I'm trying to solve the same issue as jeffam in #11. In fact, I was trying to write a module that would let users specify which of several redundant menu links would act as the "canonical" one. In digging through the code to figure out how Drupal decided which is the preferred menu link, I came to this query and noticed there's currently no way to hook into it, or alter the menu_link_get_preferred output in any way.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
David_Rothstein’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC assuming that was a testbot fluke.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: menu.inc-preferred_menu_links_tag-1854134-11.patch, failed testing.

Status: Needs work » Needs review
dcam’s picture

Status: Needs review » Reviewed & tested by the community
David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +7.33 release notes

Committed to 7.x - thanks!

(And yeah, I think there is no aspect of this patch that is relevant for Drupal 8 anymore.)

  • David_Rothstein committed 56e257d on 7.x
    Issue #1854134 by EtienneRd, jeffam | dolu: Added a query tag to the...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.