Fragmented Thought

Drupal - Build menu using Menu_Block directly

By

Published

Lance Gliser

Heads up! This content is more than six months old. Take some time to verify everything still works as expected.

I run across the need for some very specific menus built on the fly for Drupal once in a while. Things that really aren't blocks that should be running around the block admin. A great example would be ajax calls to obtain the submenus for an item and add them to the page post render for seo purposes.

If you need to access a very custom part of some menu, use the menu_block module, and tweak the following code to your needs:

$settings = array( 'menu_name' => 'main-menu', 'expanded' => 1, // Expand all children of this tree. 'level' => 1, // Starting level 'follow' => 0, // Make the starting level follow the active menu item. 'depth' => 2, // Maximum depth - 0 is unlimited 'sort' => 0, // Sort menu tree by the active menu item’s trail. 'parent_mlid' => 0, // The menu item to start from - 0 is root 'title_link' => 0, // If the title should be a link or not 'delta' => 0, // Which menu block this is ); $menu_block = menu_tree_build($settings); print render($menu_block['content']);