Last updated March 6, 2013. Created by JohnAlbin on March 20, 2010.
Edited by Gerald Mengisen, zifiniti. Log in to edit this page.

Adding menu blocks

To add new menu blocks, use the "Add menu block" tab (or button) on the administer blocks page, admin/build/block. You will then be able to configure your menu block before adding it.

Configuring menu blocks

When adding or configuring a menu block, several configuration options are available:

Block title
For menu trees that start with the 1st level, the default block title will be the menu name. For menu trees that start with the 2nd level or deeper, the default block title will be the title for the parent menu item of the specified level.

For example, if the active menu trail for the Navigation menu is: Administer > Site building > Menus > Primary links, then a menu block configured to start with the 1st level of the Navigation will display a block title of "Navigation". And a menu block configured to start with the 3rd level of the Navigation menu will display a block title of "Site building".

Block title as link
For menu trees that start with the 2nd level or deeper, the default block title will be the title for the parent menu item of the specified level. If this option is checked, the block title will be a link to that menu item. Note: If a block title has been overridden, this option will not be available.
Parent item
First select the menu. Then select the parent item from that menu. The tree of links will only contain children of the selected parent item.
Starting level
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.
Make the starting level follow the active menu item
If the active menu item is deeper than the level specified above, the starting level will follow the active menu item. Otherwise, the starting level of the tree will remain fixed.
Maximum depth
From the starting level, specify the maximum depth of the tree. Blocks with a maximum depth of 1 will just be a single un-nested list of links with none of those links' children displayed.
Expand children
All children of this menu will be expanded.
Sort
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.

Styling menu blocks

Classes:

Themers should look at the myriad of classes added to the <div>, <li> and <a> elements.

<div>
The <div> wrapped around the menu tree has a class for several of the configurable options of the block: menu-block-[block id number] menu-name-[menu name] parent-mlid-[menu link ID] menu-level-[level number]
<li>
The <li> elements of the menu tree can have an extended list of classes (compared to standard menu trees): first last menu-mlid-[menu link ID] has-children active active-trail
<a>
The <a> elements of the menu tree can have: active active-trail

Templates:

In addition, the wrapper <div> for the block is generated using the menu-block-wrapper.tpl.php template. And Menu block provides several theme hook suggestions for that template:

  • menu-block-wrapper--[block id number].tpl.php
  • menu-block-wrapper--[menu name].tpl.php

For example, a file in your theme called menu-block-wrapper--primary-links.tpl.php can be used to override the <div> for just the "Primary links" menu blocks.

Theme functions:

Menu block uses Drupal core's menu theme functions. However, it also provides theme hook suggestions that can be used to override any of the theme functions called by it.

  • theme_menu_tree() can be overridden by creating one of:
    • [theme]_menu_tree__menu_block()
    • [theme]_menu_tree__menu_block__[menu name]()
    • [theme]_menu_tree__menu_block__[block id number]()
  • theme_menu_item() can be overridden by creating one of:
    • [theme]_menu_item__menu_block()
    • [theme]_menu_item__menu_block__[menu name]()
    • [theme]_menu_item__menu_block__[block id number]()
  • theme_menu_item_link() can be overridden by creating one of:
    • [theme]_menu_item_link__menu_block()
    • [theme]_menu_item_link__menu_block__[menu name]()
    • [theme]_menu_item_link__menu_block__[block id number]()

For example, if you created a garland_menu_tree__menu_block() function, it would override theme_menu_block() any time it was used by this module, but not when used by any other module. Similarly, a garland_menu_item__menu_block__1() function would override theme_menu_item(), but only for the first menu block in your system (the menu block with an ID of 1).

Menu block API

Developers can use the API of this module to create their own menu trees outside the confines of blocks. All of the publicly available API functions are documented in the menu_block.module file.

In addition, Menu block implements hook_get_menus() and hook_menu_block_tree_alter(). See menu_block.api.php for documentation.

Further information

John Albin, the author of the module, wrote two great articles that explain Menu Block even more: Better Know a Module: Menu Block (Part I and Part II)

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

>>Parent item
>> First select the menu. Then select the parent item from that menu. The tree of links will only contain children of the selected parent item.

How to get know a title of parent item?

Pristine installation, Bartik, and no other module installed than menu-block.
Two basic pages FOO and BAR, both assigned to the Main Menu (so they appear as tabs on the top, next to Home).

If I understand the documentation correctly, it should be possible to create two menus, called FOO-menu and BAR-menu, and make it so that when FOO is opened the FOO-menu appears, but when BAR is opened the FOO-menu is replaced with the BAR-menu.
Have I understood this correctly?
And, if so, how do I do that?

Sorry if this is obvious.

[TaSK]

Tage-SK
Pisa
Italy

This module uses Drupal's default Main Menu system, so your pages/links need to exist in that menu first. Then, instead of adding the Main Menu block to your pages, you add the Menu Block block to your pages.

Confused, theme_menu_item is gone in D7. Yes?
Looks like its just this now...

- theme_menu_link() can be overridden by creating one of:
  - [theme]_menu_link__[menu name]()
  - [theme]_menu_link__menu_block()
  - [theme]_menu_link__menu_block__[menu name]()
  - [theme]_menu_link__menu_block__[block id number]()

Where is the custom path configuration? Seems that all of those features that existed for Taxonomy Menu + Taxonomy Block are missing in D7.

Don't see how this is any different than creating a custom (core) menu and assigning a taxonomy menu to that menu block. Please explain.

I think this is D7's equivalent now theme_menu_link

I think this is D7's equivalent now theme_menu_link

I use this module, too but how i can give the first main items in the link ... a css class via template?

I need following source code:

<nav>
<ul id="nav" role="navigation">
<li class="top-level item-with-ul"><a href="" class="link-with-ul">Item #1</a>
<ul class="sub-menu">
<li><a href="#content">Sub 1 Item #1</a></li>
<li><a href="">Sub 1 Item #2</a></li>
<li><a href="">Sub 1 Item #3</a></li>
<li><a href="">Sub 1 Item #4</a></li>
</ul>
</li>
<li class="top-level item-with-ul"><a href="" class="link-with-ul">Item #2</a>
<ul class="sub-menu">
<li><a href="">Sub 1 Item #1</a></li>
<li><a href="">Sub 1 Item #2</a></li>
<li><a href="">Sub 1 Item #3</a></li>
</ul>
</li>
<li class="top-level"><a href="">Item #3</a>
</li>
<li class="top-level item-with-ul"><a href="" class="link-with-ul">Item #4</a>
<ul class="sub-menu">
<li><a href="">Sub 1 Item #1</a></li>
<li><a href="">Sub 1 Item #1</a></li>
<li><a href="">Sub 1 Item #1</a></li>
</ul>
</li>
<li class="top-level"><a href="">Item #5</a>
</li>
</ul>
</nav>

neofelis - Internet-agency for Webdevelopment and Internet-Marketing
Visit us on http://www.neofelis.de