Splitting off second level navigation into a separate block
henrrrik - February 13, 2007 - 10:11
Hi,
How do I go about moving the second level navigation into a separate block without breaking the breadcrumb trail? I have a site that should feature a horizontal first level navigation in the header and a vertical second level navigation in the left sidebar (see saabgroup.com for an example).
Faking a second level by creating a number of separate vertical menus that only appear on given pages doesn't generate the desired breadcrumb trail (rightly so since they're not really subpages) and makes it messy and complicated to maintain.
Is some theming stunt the way to go?

Your best bet is probably
Your best bet is probably the custom breadcrumb module: http://drupal.org/project/custom_breadcrumbs
try $secondary_links
Had a very similar need-- wanted primary nav up top, secondary nav on side-bar (in my case, the right block)-- here's the code I use:
<?php if ($sidebar_right) { ?><?php if ($secondary_links) {
print 'refine your choices by:'. theme('menu_links', $secondary_links);
} ?>
<?php print $sidebar_right ?>
<?php } ?>
You probably just want the interior IF block referencing $secondary_links, but might be nice to do the outside IF test for $sidebar_right (or left) as well.
Note i modified the above from my default theme, but I'm sorta new at PHP and frankly don't quite understand why some people enclose every single line inside PHP brackets while some don't. For instance, IMO it might look cleaner with just a nested IF inside a single PHP statement w/out all the explicit PHP bracketing... not sure how legal that is 'cause I haven't tested it, but you also might experiment with something like:
<?phpif ($sidebar_right) {
if ($secondary_links) {
print 'refine your choices by:'. theme('menu_links', $secondary_links);
}
print $sidebar_right;
}
?>
Again, the 1st code block here is verbatim and works on my site as-is, the 2nd i just included here as an alt way of saying the same thing but haven't tested myself.
Thanks! That's probably the
Thanks! That's probably the easiest way to proceed for now. It doesn't seem to allow for a tertiary level though. Also, it would be nice to be able to split a regular menu into separate HTML lists for each level of navigation, as blocks or otherwise.
Regarding PHP tags, the PHP interpreter starts parsing when it finds a start tag and stops when it finds a stop tag, so marking up every line/instruction individually as PHP is redundant.
My crude solution
I managed to hack together a crude solution to this problem. Perhaps someone with more insight into the inner workings of the Drupal menu system can help clean this up?
Note that this only works with two levels of navigation.
This chunk of code outputs the first level navigation of the menu:
<?php
/** This displays the first level **/
$menu_to_split="My menu"; /* This is the name of the menu that should be split! */
$menu = menu_get_menu();
foreach ($menu['visible'] as $test) {
if( $test['title']==$menu_to_split ) {
print "<ul class=\"menu\">\n";
foreach($test[children] as $child) {
print "<li>".menu_item_link($child)."</li>\n";
}
print "</ul>\n";
}
}
?>
And this outputs the second level:
<?php
/** Display second level **/
$menu_to_split="My menu"; /* This is the name of the menu that should be split! */
$menu = menu_get_menu();
$trail = _menu_get_active_trail();
$active = menu_get_active_item();
$temp=menu_get_item($trail[0]);
$menuname=$temp['title'];
if(count($trail)>2 && $menuname==$menu_to_split) {
$topitem=menu_get_item($trail[1]);
foreach ($menu['visible'] as $menuitem) {
if($menuitem['title']==$topitem['title'] ) {
print "<ul class=\"menu\">\n";
foreach($menuitem[children] as $child) {
print "<li>".menu_item_link($child)."</li>\n";
}
print "</ul>\n";
}
}
}
?>
I also used this phptemplate snippet to sort out the active class stuff: http://drupal.org/node/87902
Edit: Fixed a bug in the second snippet.
How is this actually working
How is this actually working for you? I'm trying it with two blocks showing first and second level of Primary links but something is wrong. First level block shows fine. However, when I click on a first level menu item, second level menu doesn't show up, which I was expecting. Second level block only shows up when actually a second level item is already selected.
---
Robert Garrigos
Professional site: robert.garrigos.cat
Catalan Drupal Users Group: drupal.cat
I found a very easy way to
I found a very easy way to have this functionality with Primary links by using this code for a php block:
<?php
$links = menu_primary_links(1,0);
if ($links) {
$output = theme('links', $links, array('class' => 'list'));
}
return $output;
?>
For a second level menu block use the same code with a small change:
$links = menu_primary_links(2,0);---
Robert Garrigos
Professional site: robert.garrigos.cat
Catalan Drupal Users Group: drupal.cat
Superb! That's obviously the
Superb! That's obviously the proper way to do it.
What about if I want the third level of navigation to appear nested in the second level?
An example:
Block 1 - 1st level:
Block 2 - 2nd and 3rd level:
Is there a tidy way of accomplishing this?
code for the first bloc. It
code for the first bloc. It will show first level only for the primary links menu. Note the different class used here.
<?php
$links = menu_primary_links(1);
if ($links) {
$output = theme('links', $links, array('class' => 'menu'));
}
return $output;
?>
Code for the second bloc. It will show second level of active primary links menu and all child levels, not just the third one.
<?php
$links = menu_primary_links(2);
if ($links) {
$key = key($links);
$ekey = explode('-', $key);
$pid = $ekey[3];
$output = theme_menu_tree($pid);
return $output;
}
?>
---
Robert Garrigos
Professional site: robert.garrigos.cat
Catalan Drupal Users Group: drupal.cat
I created a php snippet
I created a php snippet handbook page to comment on this if you like: http://drupal.org/node/142352
---
Robert Garrigos
Professional site: robert.garrigos.cat
Catalan Drupal Users Group: drupal.cat
Great, thanks!
Great, thanks!
I finally created a new
I finally created a new module for this: http://drupal.org/project/menu_block_split
---
Robert Garrigos
Professional site: robert.garrigos.cat
Catalan Drupal Users Group: drupal.cat
THANKS!!!
Your code is VERY good and is in fact the best I have seen to accomplish this task. THANK YOU VERY MUCH!!!! +10
Class="active" ?
i manage to split first/second and third level into different blocks but i can't get a class="active" to all active links (child and all of its anchestors).
Any ideas?
First level not active
The problem ist, that the first level is not marked as active when you're in the third level...
is this still that way in
is this still that way in drupal 5.7?
Hi I think that
Hi
I think that taxonomy_dhtml can do it.
Mahjong Online Games