PDO::FETCH_ASSOC)); $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path'); $query->fields('ml'); // Weight must be taken from {menu_links}, not {menu_router}. $query->addField('ml', 'weight', 'link_weight'); $query->fields('m'); $query->condition('ml.link_path', $path_candidates, 'IN'); // Sort candidates by link path and menu name. $candidates = array(); foreach ($query->execute() as $candidate) { $candidate['weight'] = $candidate['link_weight']; $candidates[$candidate['link_path']][$candidate['menu_name']] = $candidate; } // Pick the most specific link, in the most preferred menu. $preferred_link = FALSE; foreach ($path_candidates as $link_path) { if (isset($candidates[$link_path])) { foreach ($menu_names as $menu) { if (!$preferred_links[$path][$menu] && isset($candidates[$link_path][$menu])) { $candidate_item = $candidates[$link_path][$menu]; $map = explode('/', $path); _menu_translate($candidate_item, $map); if ($candidate_item['access']) { $preferred_links[$path][$menu] = $candidate_item; if (!$preferred_link) { $preferred_link = $candidate_item; } } } } } } if ($menu_name === '' && $preferred_link) { $preferred_links[$path][$menu_name] = $preferred_link; } } return $preferred_links[$path][$menu_name]; }