diff --git includes/menu.inc includes/menu.inc index 38e4f77..990dfdc 100644 --- includes/menu.inc +++ includes/menu.inc @@ -1115,6 +1115,47 @@ function menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL) { } /** + * Set the path for determining the active trail of the specified menu tree. + * + * This path will also affect the breadcrumbs under some circumstances. + * Breadcrumbs are built using the preferred link returned by + * menu_link_get_preferred(). If the preferred link is inside one of the menus + * specified in calls to menu_tree_set_path(), the preferred link will be + * overridden by the corresponding path returned by menu_tree_get_path(). + * + * Setting this path does not affect the main content; for that use + * menu_set_active_item() instead. + * + * @param $menu_name + * The name of the affected menu tree. + * @param $path + * The path to use when finding the active trail. + */ +function menu_tree_set_path($menu_name, $path) { + return menu_tree_get_path($menu_name, $path); +} + +/** + * Get the path for determining the active trail of the specified menu tree. + * + * @param $menu_name + * The menu name of the requested tree. + * @param $path + * Internal use only. + * + * @return + * A string containing the path. If no path has been specified with + * menu_tree_set_path(), NULL is returned. + */ +function menu_tree_get_path($menu_name, $path = NULL) { + $paths = &drupal_static(__FUNCTION__); + if (isset($path)) { + $paths[$menu_name] = $path; + } + return isset($paths[$menu_name]) ? $paths[$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 @@ -1139,8 +1180,10 @@ function menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL) { function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE) { $tree = &drupal_static(__FUNCTION__, array()); + // Check if the active trail has been overridden for this menu tree. + $active_path = menu_tree_get_path($menu_name); // Load the menu item corresponding to the current page. - if ($item = menu_get_item()) { + if ($item = menu_get_item($active_path)) { if (isset($max_depth)) { $max_depth = min($max_depth, MENU_MAX_DEPTH); } @@ -1179,8 +1222,9 @@ function menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = // If the item for the current page is accessible, build the tree // parameters accordingly. if ($item['access']) { - // Find a menu link corresponding to the current path. - if ($active_link = menu_link_get_preferred()) { + // Find a menu link corresponding to the current path. If $active_path + // is NULL, let menu_link_get_preferred() determine the path. + if ($active_link = menu_link_get_preferred($active_path)) { // 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 @@ -2210,38 +2254,36 @@ function menu_get_active_menu_names() { /** * Set the active path, which determines which page is loaded. * - * @param $path - * A Drupal path - not a path alias. - * * Note that this may not have the desired effect unless invoked very early * in the page load, such as during hook_boot, or unless you call * menu_execute_active_handler() to generate your page output. + * + * @param $path + * A Drupal path - not a path alias. */ function menu_set_active_item($path) { $_GET['q'] = $path; } /** - * Sets or gets the active trail (path to menu tree root) of the current page. + * Sets the active trail (path to menu tree root) of the current page. + * + * Any trail set by this function will only be used for functionality that calls + * menu_get_active_trail(). Drupal core only uses trails set here for + * breadcrumbs and the page title and not for menu trees or page content. + * Additionally, breadcrumbs set by drupal_set_breadcrumb() will override any + * trail set here. + * + * To affect the trail used by menu trees, use menu_tree_set_path(). To affect + * the page content, use menu_set_active_item() instead. * * @param $new_trail - * Menu trail to set, or NULL to use previously-set or calculated trail. If - * supplying a trail, use the same format as the return value (see below). + * Menu trail to set; the value is saved in a static variable and can be + * retrieved by menu_get_active_trail(). The format of this array should be + * the same as the return value of menu_get_active_trail(). * * @return - * Path to menu root of the current page, as an array of menu link items, - * starting with the site's home page. Each link item is an associative array - * with the following components: - * - title: Title of the item. - * - href: Drupal path of the item. - * - localized_options: Options for passing into the l() function. - * - type: A menu type constant, such as MENU_DEFAULT_LOCAL_TASK, or 0 to - * indicate it's not really in the menu (used for the home page item). - * If $new_trail is supplied, the value is saved in a static variable and - * returned. If $new_trail is not supplied, and there is a saved value from - * a previous call, the saved value is returned. If $new_trail is not supplied - * and there is no saved value, the path to the current page is calculated, - * saved as the static value, and returned. + * The active trail. See menu_get_active_trail() for details. */ function menu_set_active_trail($new_trail = NULL) { $trail = &drupal_static(__FUNCTION__); @@ -2398,7 +2440,20 @@ function menu_link_get_preferred($path = NULL) { /** * Gets the active trail (path to root menu root) of the current page. * - * See menu_set_active_trail() for details of return value. + * If a trail is supplied to menu_set_active_trail(), that value is returned. If + * a trail is not supplied to menu_set_active_trail(), the path to the current + * page is calculated and returned. The calculated trail is also saved as a + * static value for use by subsequent calls to menu_get_active_trail(). + * + * @return + * Path to menu root of the current page, as an array of menu link items, + * starting with the site's home page. Each link item is an associative array + * with the following components: + * - title: Title of the item. + * - href: Drupal path of the item. + * - localized_options: Options for passing into the l() function. + * - type: A menu type constant, such as MENU_DEFAULT_LOCAL_TASK, or 0 to + * indicate it's not really in the menu (used for the home page item). */ function menu_get_active_trail() { return menu_set_active_trail(); diff --git modules/simpletest/tests/menu.test modules/simpletest/tests/menu.test index acbd7c8..fff8279 100644 --- modules/simpletest/tests/menu.test +++ modules/simpletest/tests/menu.test @@ -1398,3 +1398,122 @@ class MenuBreadcrumbTestCase extends DrupalWebTestCase { return $parts; } } + +/** + * Active menu trail related tests. + */ +class MenuTrailTestCase extends MenuBreadcrumbTestCase { + public static function getInfo() { + return array( + 'name' => 'Menu trail display', + 'description' => 'Tests menu trail display and alteration functions.', + 'group' => 'Menu', + ); + } + + function setUp() { + parent::setUp(); + + // This test puts menu links in the Management menu and then tests for + // their presence on the page, so we need to ensure that the Management + // block will be displayed in all active themes. + db_update('block') + ->fields(array( + // Use a region that is valid for all themes. + 'region' => 'content', + 'status' => 1, + )) + ->condition('module', 'system') + ->condition('delta', 'management') + ->execute(); + } + + /** + * Prevents parent class' tests from running. + */ + function testBreadCrumbs() { + } + + /** + * Tests active trails are properly affected by menu_tree_set_path(). + */ + function testMenuTreeSetPath() { + $home = array('' => 'Home'); + $config = $home + array( + 'admin' => t('Administration'), + 'admin/config' => t('Configuration'), + ); + + // The menu_test_menu_tree_set_path system variable controls whether or not + // the menu_test_menu_trail_callback() callback (used by all paths in these + // tests) issues an overrding call to menu_trail_set_path(). + $test_menu_path = array( + 'menu_name' => 'management', + 'path' => 'admin/config/system/site-information', + ); + + $case1_breadcrumb = $home + array( + 'menu-test' => t('Menu test root'), + ); + $case1_tree = array( + 'menu-test' => t('Menu test root'), + 'menu-test/menu-trail' => t('Menu trail - Case 1'), + ); + + $case2_breadcrumb = $config + array( + 'admin/config/development' => t('Development'), + ); + $case2_tree = $case2_breadcrumb + array( + 'admin/config/development/menu-trail' => t('Menu trail - Case 2'), + ); + array_shift($case2_tree); // Remove the Home link from the tree array. + + $case2_override_breadcrumb = $config + array( + 'admin/config/system' => t('System'), + 'admin/config/system/site-information' => t('Site information'), + ); + $case2_override_tree = $case2_override_breadcrumb; + array_shift($case2_override_tree); // Remove the Home link from the tree array. + + // Test the tree generation for the Navigation menu. + variable_del('menu_test_menu_tree_set_path'); + $this->assertTrail('menu-test/menu-trail', $case1_breadcrumb, t('Menu trail - Case 1'), $case1_tree); + + // Override the active trail for the Management tree; it should not affect + // the Navigation tree. + variable_set('menu_test_menu_tree_set_path', $test_menu_path); + $this->assertTrail('menu-test/menu-trail', $case1_breadcrumb, t('Menu trail - Case 1'), $case1_tree); + + // Test the tree generation for the Management menu. + variable_del('menu_test_menu_tree_set_path'); + $this->assertTrail('admin/config/development/menu-trail', $case2_breadcrumb, t('Menu trail - Case 2'), $case2_tree); + + // Override the active trail for the Management tree; it should affect the + // breadcrumbs and Management tree. + variable_set('menu_test_menu_tree_set_path', $test_menu_path); + $this->assertTrail('admin/config/development/menu-trail', $case2_override_breadcrumb, t('Menu trail - Case 2'), $case2_override_tree); + } + + /** + * Assert that a given path shows certain breadcrumb links. + * + * @param string $goto + * (optional) A system path to pass to DrupalWebTestCase::drupalGet(). + * @param array $breadcrumb + * An associative array whose keys are expected breadcrumb link paths and + * whose values are expected breadcrumb link texts (not sanitized). + * @param string $page_title + * (optional) A page title to additionally assert via + * DrupalWebTestCase::assertTitle(). Without site name suffix. + * @param array $tree + * (optional) An associative array whose keys are link paths and whose + * values are link titles (not sanitized) of an expected active trail in a + * menu tree output on the page. + * @param array $last_active + * (optional) Whether the last link in $tree is expected to be active (TRUE) + * or just to be in the active trail (FALSE). + */ + protected function assertTrail($goto, array $breadcrumb, $page_title = NULL, array $tree = array(), $last_active = TRUE) { + $this->assertBreadcrumb($goto, $breadcrumb, $page_title, $tree, $last_active); + } +} diff --git modules/simpletest/tests/menu_test.module modules/simpletest/tests/menu_test.module index 4c38502..82e17dc 100644 --- modules/simpletest/tests/menu_test.module +++ modules/simpletest/tests/menu_test.module @@ -211,6 +211,20 @@ function menu_test_menu() { 'type' => MENU_LOCAL_TASK, ) + $base; + // Menu trail tests. + // @see MenuTrailTestCase + $items['menu-test/menu-trail'] = array( + 'title' => 'Menu trail - Case 1', + 'page callback' => 'menu_test_menu_trail_callback', + 'access arguments' => array('administer site configuration'), + ); + $items['admin/config/development/menu-trail'] = array( + 'title' => 'Menu trail - Case 2', + 'description' => 'Tests menu_tree_set_path()', + 'page callback' => 'menu_test_menu_trail_callback', + 'access arguments' => array('administer site configuration'), + ); + // File inheritance tests. This menu item should inherit the page callback // system_admin_menu_block_page() and therefore render its children as links // on the page. @@ -325,6 +339,17 @@ function menu_test_callback() { } /** + * Callback that test menu_test_menu_tree_set_path(). + */ +function menu_test_menu_trail_callback() { + $menu_path = variable_get('menu_test_menu_tree_set_path', array()); + if (!empty($menu_path)) { + menu_tree_set_path($menu_path['menu_name'], $menu_path['path']); + } + return 'This is menu_test_menu_trail_callback().'; +} + +/** * Page callback to use when testing the theme callback functionality. * * @param $inherited