Hi guys!

while developing a my own custom theme for drupal 6.6 I got stuck, because I don't know how to get the parent of the current node and its submenu to render it in a navigation vertical left sidebar.

I explain the problem: I am using the primary links menu to organize the content of my website, and I am displaying it entirely and correctly in a horizontal navigation bar. What I need now is a vertical navigation bar in the left sidebar of the website, and it should display only a submenu of it:

my primary links menu structure is:

Home
History
--history1
--history2
----history21
----history22
--history3
About us
--overview

the following three cases are the use cases:

1) when I go in the history1 node then in the sidebar they have to be:

-----------
Hystory (i.e. the name of the current node's parent)
-----------
history1
history2 (i.e. the name and the link of the current node)
history3

2) when I go in the history2 node then in the sidebar they have to be:

---------
history2
---------
history21
history22

3) when I go in the history21 node then in the sidebar they have to be:

---------
history2
---------
history21
history22

So, my question is: how can I do that? :)

Best regards
Michele

Comments

Ujval Shah’s picture

Hi Michele,

I may help you on this, but i need to know what is your solution architecture?
are you using taxonomy or creating a simple page and assigning path of page to a menu-item?

Best,
Drupal Rocking

Anonymous’s picture

hello, thanks for your answer!

I am creating simple pages and adding their paths to a menu: the primary links menu to be more precise.
I am not using taxonomy.

Best regards
Michele

Ujval Shah’s picture

Hi Michele,

option1:
Why don't you use the book module.It provides the same feature you are looking for.
It provides the capability to have navigation,"Book navigation" block with two different option.(Show block on all pages,
Show block only on book pages).

here is the implementation using same idea.
http://luxurycouncil.com/ (about menu)

option2:
Menu Trails module may help you since you are not using the taxonomy - http://drupal.org/project/menu_block_split

Let me know any of the option will help you or not,i have few other options too to implement this.

Best,
Drupal Rocking

Anonymous’s picture

Hello, thanks for your answer!

I solved that by myself by using some crafted functions who query the menu table in the drupal db, using the node path as key: they works good for me, and due the deadline I think I'll keep them.

Anyway I thank you again for telling me about the menu_block_module: I think it will be worth looking at its code and hack on it. :)

Best regards
Michele

NidSquid’s picture

Hello, can you please share your solution? I'm trying to do the same thing. Thanks.

jpfeifer’s picture

I am looking for this as well. I can't believe that this isn't an easier thing to do already.

arh1’s picture

after poring over the API for hours, i think i've got a custom bit of code (read: hack) in place to pull this off for Drupal 6.

to reiterate, my scenario is that i have one comprehensive site menu as my Primary Links. i want to display the entire menu via Nice Menus at the top of my theme, but show context-specific "submenus" (starting one level down from the primary links, and showing the entire tree underneath) in blocks.

the hack is based on a modified version of Drupal's menu_navigation_links function, that stops short and returns the multi-level menu tree array, rather than continuing on to return the single level links array. we can then pass the returned tree through menu_tree_output to get the markup.

the second argument of the function specifies that we want to start one level below the primary links.

(hat tip to http://www.freesoftwaremagazine.com/books/drupal_tricks/tertiary_menu for pointing me in the right direction.)

i'd love to hear how this compares to your solution, Michele, or if anyone else can point out something simpler that i'm missing!

<?php
function mytheme_menu_navigation_links($menu_name, $level = 0) {
  // Don't even bother querying the menu table if no menu is specified.
  if (empty($menu_name)) {
    return array();
  }

  // Get the menu hierarchy for the current page.
  $tree = menu_tree_page_data($menu_name);

  // Go down the active trail until the right level is reached.
  while ($level-- > 0 && $tree) {
    // Loop through the current level's items until we find one that is in trail.
    while ($item = array_shift($tree)) {
      if ($item['link']['in_active_trail']) {
        // If the item is in the active trail, we continue in the subtree.
        $tree = empty($item['below']) ? array() : $item['below'];
        break;
      }
    }
  }
  return $tree;
}

$tree = mytheme_menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), 1);
print menu_tree_output($tree);
?>
Ujval Shah’s picture

Can you share the site url, where you have implemented this.
This will help other community member to try the demonstration too.

arh1’s picture

sorry, the site's not public yet. but as far as what you'll see rendered to the page, it's pretty simple to describe:

1) the entire site menu is contained in the Primary Links which are printed at the top of the page (i happen to be using nice_menus to display those in a manageable way, but that's beside the point)

2) the sidebar includes a block that only shows the "submenu" of the "section" you're in (in my case, that means "tier 2" links and below in the menu tree)

note that this is subtly different than the original use cases listed above. in my case, whether i'm at 'History', 'history2', or 'history22', i will see the following in my "submenu" in the sidebar:

History
--history1
--history2
----history21
----history22
--history3

here's the code, cleaned up a bit. first, in template.php:

<?php
/**
 * Hack to derive a submenu tree from a larger menu.
 * This is a modified version of Drupal's menu_navigation_links function, that 
 * stops short and returns the multi-level menu tree array, rather than 
 * continuing on to return the single level links array. We can then pass the
 * returned tree through menu_tree_output to get the markup.
 *
 * @param 
 *   $menu_name The name of the menu.
 *
 * @param 
 *   $level Optional, the depth of the menu to be returned.
 *
 * @return
 *   A submenu tree array.
 */
function menu_navigation_tree($menu_name, $level = 0) {
  // Don't even bother querying the menu table if no menu is specified.
  if (empty($menu_name)) {
    return array();
  }

  // Get the menu hierarchy for the current page.
  $tree = menu_tree_page_data($menu_name);

  // Go down the active trail until the right level is reached.
  while ($level-- > 0 && $tree) {
    // Loop through the current level's items until we find one that is in trail.
    while ($item = array_shift($tree)) {
      if ($item['link']['in_active_trail']) {
        // If the item is in the active trail, we continue in the subtree.
        $tree = empty($item['below']) ? array() : $item['below'];
        break;
      }
    }
  }
  return $tree;
}
?>

then, in my sidebar "submenu" block:

<?php
$tree = menu_navigation_tree(variable_get('menu_primary_links_source', 'primary-links'), 1);
print menu_tree_output($tree);
?>
?>
tanc’s picture

Thanks arh1, that code is perfectly reusable and works nicely for a project I'm working on.

JohnAlbin’s picture

http://drupal.org/project/menu_block does what you are wanting.

  - John (JohnAlbin)

arh1’s picture

thanks, JohnAlbin.

i use the word "hack" here somewhat loosely -- my code above doesn't feel bad in that it's easy enough to maintain at the theme level. personally, i always prefer that to another contrib module, where i'm less confident i'll be able to maintain it should the developers go away, lose interest, etc.

that said, menu_block looks pretty great, and like you, i'm shocked this module didn't exist before. and clearly this module is maintained by a reputable and invested developer :) thanks for pointing it out.

kitsunechan’s picture

To me it sounds like you might like the Local Menu Module. When you go to the link the explanation of the module is a little weird, but I think it is the solution you are looking for.
http://drupal.org/project/local_menu

arh1’s picture

good to know! that does indeed look like a good match. for my part, i prefer to handle this type of thing w/ my own code at the theme level if possible, but thanks for the pointer.

jaydubb181’s picture

JohnAlbin’s picture

I've put a comparison of the two modules on http://drupal.org/project/menu_block

  - John (JohnAlbin)