diff --git a/menu_block.admin.inc b/menu_block.admin.inc index 98f3bbb77be7ab5dbab8ef6071823c6e69aa182a..d7bf10035dff3ac4bad3ae81d4bf031cc421b451 100644 --- a/menu_block.admin.inc +++ b/menu_block.admin.inc @@ -166,6 +166,8 @@ function menu_block_delete_submit($form, &$form_state) { variable_del("menu_block_{$delta}_depth"); variable_del("menu_block_{$delta}_expanded"); variable_del("menu_block_{$delta}_sort"); + variable_del("menu_block_{$delta}_hide_siblings"); + variable_del("menu_block_{$delta}_active_depth"); db_delete('block') ->condition('module', 'menu_block') @@ -423,6 +425,12 @@ function menu_block_configure_form($form, &$form_state) { '#default_value' => $config['sort'], '#description' => t('Sort each item in the active trail to the top of its level. When used on a deep or wide menu tree, the active menu item’s children will be easier to see when the page is reloaded.'), ); + $form['hide_siblings'] = array( + '#type' => 'checkbox', + '#title' => t('Hide siblings of the active menu item’s trail.'), + '#default_value' => $config['hide_siblings'], + '#description' => t('If a menu item isn\'t in the active trail, don\'t show it.'), + ); $form['parent_mlid'] = array( '#type' => 'select', '#title' => t('Fixed parent item'), @@ -439,7 +447,7 @@ function menu_block_configure_form($form, &$form_state) { foreach (array('title_link', 'follow', 'follow_parent', 'expanded', 'sort', 'parent_mlid') as $key) { $form[$key]['#states']['visible'][':input[name=display_options]'] = array('value' => 'advanced'); } - if ($config['title_link'] || $follow || $config['expanded'] || $config['sort'] || $config['parent_mlid']) { + if ($config['title_link'] || $follow || $config['expanded'] || $config['sort'] || $config['hide_siblings'] || $config['parent_mlid']) { $form['display_options']['#default_value'] = 'advanced'; } @@ -492,6 +500,7 @@ function _menu_block_block_save($delta = '', $edit = array()) { variable_set("menu_block_{$delta}_depth", $edit['depth']); variable_set("menu_block_{$delta}_expanded", $edit['expanded']); variable_set("menu_block_{$delta}_sort", $edit['sort']); + variable_set("menu_block_{$delta}_hide_siblings", $edit['hide_siblings']); } } } diff --git a/menu_block.module b/menu_block.module index 83cf0c98d475ddaf7ee1abd95cb384a026985131..c03ee69189f2e4ea8f42543aca46ac75722d3f75 100644 --- a/menu_block.module +++ b/menu_block.module @@ -192,6 +192,8 @@ function menu_block_get_config($delta = NULL) { 'depth' => 0, 'expanded' => 0, 'sort' => 0, + 'hide_siblings' => 0, + 'active_depth' => array(), ); // Get the block configuration options. @@ -216,6 +218,8 @@ function menu_block_get_config($delta = NULL) { $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['hide_siblings'] = variable_get("menu_block_{$delta}_hide_siblings", $config['hide_siblings']); + $config['active_depth'] = variable_get("menu_block_{$delta}_active_depth", $config['active_depth']); list($config['menu_name'], $config['parent_mlid']) = explode(':', variable_get("menu_block_{$delta}_parent", $config['menu_name'] . ':' . $config['parent_mlid'])); } @@ -352,6 +356,11 @@ function menu_tree_build($config) { if ($config['sort']) { menu_tree_sort_active_path($tree); } + + // When hiding siblings, level 1 is always in the active depth. + if ($config['hide_siblings']) { + $config['active_depth'][0] = 1; + } // Render the tree. $data = array(); @@ -643,6 +652,17 @@ function menu_block_tree_output(&$tree, $config = array()) { // Pull out just the menu items we are going to render so that we // get an accurate count for the first/last classes. foreach ($tree as $key => &$value) { + if ($config['hide_siblings']) { + // Build array of the active trails depth. + if ($tree[$key]['link']['in_active_trail'] && $tree[$key]['link']['depth'] > 1) { + $config['active_depth'][] = $tree[$key]['link']['depth']; + } + // Hide menu items on the same depth but that are not in the active trail ie. active trail siblings. + if ( in_array($tree[$key]['link']['depth'], $config['active_depth']) && $tree[$key]['link']['in_active_trail'] == FALSE ) { + $tree[$key]['link']['hidden'] = 1; + } + } + // Render all links that aren't hidden. if (!$tree[$key]['link']['hidden']) { $items[] = $tree[$key]; }