#942782: per menu prefered links.

From: Damien Tournoud <damien@commerceguys.com>


---
 menu.inc |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

diff --git includes/menu.inc includes/menu.inc
index fb61fd3..58c3bae 100644
--- includes/menu.inc
+++ includes/menu.inc
@@ -1197,7 +1197,7 @@ function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail =
         // parameters accordingly.
         if ($item['access']) {
           // Find a menu link corresponding to the current path.
-          if ($active_link = menu_link_get_preferred()) {
+          if ($active_link = menu_link_get_preferred(NULL, $menu_name)) {
             // The active link may only be taken into account to build the
             // active trail, if it resides in the requested menu. Otherwise,
             // we'd needlessly re-run _menu_build_tree() queries for every menu
@@ -2336,6 +2336,10 @@ function menu_set_active_trail($new_trail = NULL) {
  * @param $path
  *   The path, for example 'node/5'. The function will find the corresponding
  *   menu link ('node/5' if it exists, or fallback to 'node/%').
+ * @param $menu_name
+ *   The name of a menu used to restrict the search for a prefered menu link.
+ *   If not specified, all the menus returned by menu_get_active_menu_names()
+ *   will be used.
  *
  * @return
  *   A fully translated menu link, or NULL if not matching menu link was
@@ -2343,15 +2347,15 @@ function menu_set_active_trail($new_trail = NULL) {
  *   the most preferred menu (as defined by menu_get_active_menu_names()) is
  *   returned.
  */
-function menu_link_get_preferred($path = NULL) {
+function menu_link_get_preferred($path = NULL, $menu_name = '') {
   $preferred_links = &drupal_static(__FUNCTION__);
 
   if (!isset($path)) {
     $path = $_GET['q'];
   }
 
-  if (!isset($preferred_links[$path])) {
-    $preferred_links[$path] = FALSE;
+  if (!isset($preferred_links[$path][$menu_name])) {
+    $preferred_links[$path][$menu_name] = FALSE;
 
     // Look for the correct menu link by building a list of candidate paths,
     // which are ordered by priority (translated hrefs are preferred over
@@ -2373,7 +2377,12 @@ function menu_link_get_preferred($path = NULL) {
     }
 
     // Retrieve a list of menu names, ordered by preference.
-    $menu_names = menu_get_active_menu_names();
+    if ($menu_name !== '') {
+      $menu_names = array($menu_name);
+    }
+    else {
+      $menu_names = menu_get_active_menu_names();
+    }
 
     $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
     $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
@@ -2404,14 +2413,14 @@ function menu_link_get_preferred($path = NULL) {
         $map = explode('/', $path);
         _menu_translate($candidate_item, $map);
         if ($candidate_item['access']) {
-          $preferred_links[$path] = $candidate_item;
+          $preferred_links[$path][$menu_name] = $candidate_item;
         }
         break 2;
       }
     }
   }
 
-  return $preferred_links[$path];
+  return $preferred_links[$path][$menu_name];
 }
 
 /**
