Index: menu_block.module =================================================================== --- menu_block.module +++ menu_block.module @@ -134,16 +134,17 @@ */ function menu_block_get_config($delta = NULL) { $config = array( - 'delta' => $delta, - 'menu_name' => 'main-menu', - 'parent_mlid' => 0, - 'title_link' => 0, - 'admin_title' => '', - 'level' => 1, - 'follow' => 0, - 'depth' => 0, - 'expanded' => 0, - 'sort' => 0, + 'delta' => $delta, + 'menu_name' => 'main-menu', + 'parent_mlid' => 0, + 'title_link' => 0, + 'admin_title' => '', + 'include_parent' => 0, + 'level' => 1, + 'follow' => 0, + 'depth' => 0, + 'expanded' => 0, + 'sort' => 0, ); // Get the block configuration options. @@ -161,13 +162,14 @@ $config['exported_to_code'] = TRUE; } - $config['title_link'] = variable_get("menu_block_{$delta}_title_link", $config['title_link']); - $config['admin_title'] = variable_get("menu_block_{$delta}_admin_title", $config['admin_title']); - $config['level'] = variable_get("menu_block_{$delta}_level", $config['level']); - $config['follow'] = variable_get("menu_block_{$delta}_follow", $config['follow']); - $config['depth'] = variable_get("menu_block_{$delta}_depth", $config['depth']); - $config['expanded'] = variable_get("menu_block_{$delta}_expanded", $config['expanded']); - $config['sort'] = variable_get("menu_block_{$delta}_sort", $config['sort']); + $config['title_link'] = variable_get("menu_block_{$delta}_title_link", $config['title_link']); + $config['admin_title'] = variable_get("menu_block_{$delta}_admin_title", $config['admin_title']); + $config['level'] = variable_get("menu_block_{$delta}_level", $config['level']); + $config['follow'] = variable_get("menu_block_{$delta}_follow", $config['follow']); + $config['depth'] = variable_get("menu_block_{$delta}_depth", $config['depth']); + $config['expanded'] = variable_get("menu_block_{$delta}_expanded", $config['expanded']); + $config['sort'] = variable_get("menu_block_{$delta}_sort", $config['sort']); + $config['include_parent'] = variable_get("menu_block_{$delta}_include_parent", $config['include_parent']); list($config['menu_name'], $config['parent_mlid']) = explode(':', variable_get("menu_block_{$delta}_parent", $config['menu_name'] . ':' . $config['parent_mlid'])); } @@ -284,10 +286,10 @@ if ($config['level'] > 1 || $config['parent_mlid']) { if ($config['parent_mlid']) { $parent_item = menu_link_load($config['parent_mlid']); - menu_tree_prune_tree($tree, $config['level'], $parent_item); + menu_tree_prune_tree($tree, $config['level'], $parent_item, $config['include_parent']); } else { - menu_tree_prune_tree($tree, $config['level']); + menu_tree_prune_tree($tree, $config['level'], false, $config['include_parent']); } } @@ -474,10 +476,12 @@ * int The level of the original tree that will start the pruned tree. * @param $parent_item * array The menu item that should be used as the root of the tree. + * @param $include_parent + * boolean Whether to include the direct parent item of the tree. * @return * void */ -function menu_tree_prune_tree(&$tree, $level, $parent_item = FALSE) { +function menu_tree_prune_tree(&$tree, $level, $parent_item = FALSE, $include_parent = FALSE) { if (!empty($parent_item)) { // Prune the tree along the path to the menu item. for ($i = 1; $i <= MENU_MAX_DEPTH && $parent_item["p$i"] != '0'; $i++) { @@ -488,8 +492,12 @@ if ($tree[$key]['link']['mlid'] == $plid) { menu_block_set_title($tree[$key]['link']); // Prune the tree to the children of this ancestor. - $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array(); $found_active_trail = TRUE; + if (!empty($include_parent)) { + $tree = array($key => $tree[$key]); + } else { + $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array(); + } break; } } @@ -510,8 +518,14 @@ // Get the title for the pruned tree. menu_block_set_title($tree[$key]['link']); // Prune the tree to the children of the item in the active trail. - $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array(); $found_active_trail = TRUE; + // If the current item is the direct parent of the submenu's top level + // and it shall be included, make it and its subtree the new tree. + if (!empty($include_parent) && $i == $level - 1) { + $tree = array($key => $tree[$key]); + } else { + $tree = $tree[$key]['below'] ? $tree[$key]['below'] : array(); + } break; } } @@ -521,6 +535,14 @@ break; } } + + // Remove the only item from the tree if it is the parent item configured to + // be included. + if (!empty($include_parent) + && count($tree) == 1 + && !count($tree[key($tree)]['below'])) { + $tree = array(); + } } /** Index: menu_block.admin.inc =================================================================== --- menu_block.admin.inc +++ menu_block.admin.inc @@ -446,6 +446,12 @@ ), '#description' => t('Blocks that start with the 1st level will always be visible. Blocks that start with the 2nd level or deeper will only be visible when the trail to the active menu item is in the block’s tree.'), ); + $form['include_parent'] = array( + '#type' => 'checkbox', + '#title' => t('Include parent'), + '#default_value' => $config['include_parent'], + '#description' => t('Should this block include the parent as the starting level? The parent is not included if it was the only item in the menu.'), + ); // The value of "follow" in the database/config array is either FALSE or the // value of the "follow_parent" form element. if ($follow = $config['follow']) { @@ -550,6 +556,7 @@ variable_set("menu_block_{$delta}_title_link", $edit['title_link']); variable_set("menu_block_{$delta}_admin_title", $edit['admin_title']); variable_set("menu_block_{$delta}_parent", $edit['parent']); + variable_set("menu_block_{$delta}_include_parent", $edit['include_parent']); variable_set("menu_block_{$delta}_level", $edit['level']); variable_set("menu_block_{$delta}_follow", $edit['follow']); variable_set("menu_block_{$delta}_depth", $edit['depth']);