diff --git includes/menu.inc includes/menu.inc index 642e946..b9eb444 100644 --- includes/menu.inc +++ includes/menu.inc @@ -1095,6 +1095,38 @@ function menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL) { } /** + * Set the active item for the specified menu tree. + * + * @param $menu_name + * The name of the affected menu tree. + * @param $item + * A menu item with array keys like those returned by menu_get_item(). + */ +function menu_tree_set_item($menu_name, $item) { + return menu_tree_get_item($menu_name, $item); +} + +/** + * Get the active item for the requested menu tree. + * + * @param $menu_name + * The menu name of the requested tree. + * @param $item + * Internal use only. + * + * @return + * A menu item with array keys like those returned by menu_get_item(). If no + * menu item has been specified with menu_tree_set_item(), NULL is returned. + */ +function menu_tree_get_item($menu_name, $item = NULL) { + $items = &drupal_static(__FUNCTION__); + if (isset($item)) { + $items[$menu_name] = $item; + } + return isset($items[$menu_name]) ? $items[$menu_name] : NULL; +} + +/** * Get the data structure representing a named menu tree, based on the current page. * * The tree order is maintained by storing each parent in an individual @@ -1120,7 +1152,7 @@ function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = $tree = &drupal_static(__FUNCTION__, array()); // Load the menu item corresponding to the current page. - if ($item = menu_get_item()) { + if (($item = menu_tree_get_item($menu_name)) || ($item = menu_get_item())) { if (isset($max_depth)) { $max_depth = min($max_depth, MENU_MAX_DEPTH); } @@ -1160,7 +1192,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($item['href'])) { // 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 diff --git modules/simpletest/tests/menu.test modules/simpletest/tests/menu.test index 0c002e2..3387a8f 100644 --- modules/simpletest/tests/menu.test +++ modules/simpletest/tests/menu.test @@ -562,6 +562,43 @@ class MenuTreeDataTestCase extends DrupalUnitTestCase { /** * Menu breadcrumbs related tests. */ +class MenuTreeTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Menu tree display', + 'description' => 'Tests menu tree display functions.', + 'group' => 'Menu', + ); + } + + /** + * Test the menu_tree_set_item() affects menu tree generation. + */ + function testMenuTreeSetItem() { + $path = 'admin/config/system/site-information'; + $menu = 'management'; + + // Verify the test path is not originally in the menu tree. + $tree = drupal_render(menu_tree($menu)); + $this->assert(strpos($tree, $path) === FALSE, t('Test link does not exist in menu tree.')); + + // Set the active menu tree item to the test path. + drupal_flush_all_caches(); + $item = menu_get_item($path); + menu_tree_set_item($menu, $item); + + // Check if the menu item is returned by menu_tree_get_item(). + $this->assertIdentical($item, menu_tree_get_item($menu), t('Test link set by menu_tree_set_item().')); + + // Check if the test path is in the menu tree. + $tree = drupal_render(menu_tree($menu)); + $this->assert(strpos($tree, $path) !== FALSE, t('Test link exists in menu tree.')); + } +} + +/** + * Menu breadcrumbs related tests. + */ class MenuBreadcrumbTestCase extends DrupalWebTestCase { public static function getInfo() { return array(