Add a submenu tree in a block

Warning: does not work for menu 'node/add' trail but works for 'admin'. The test done was with a custom menu. So feedback is very welcome!

You can add an expanded submenu of the primary links by creating a block containing the following code.

The end result will be a block containing a submenu of the active menu. By using block visibility the block is only visible for the appropriate submenus.

  1. Go to the admin/build/block/add page.
  2. Paste the following code into the block body:
    <?php
    $menu_trail
    = _menu_get_active_trail();
    $menu_top_level = array_shift( $menu_trail);

    // comment next line when finished testing using: // in front
    print( 'top-level-id= ' . $menu_top_level .'<br/>');

    $menu_sub_menu array_shift( $menu_trail);
    print(
    menu_tree$menu_sub_menu));
    ?>
  3. Be sure to check the PHP code.
  4. Paste the following code into 'Page specific visibility settings':
    <?php
    // Fill in the appropriate top level ID's in a comma-separated list
    $valid_top_levels = array(1, 2);
    // Uncomment next line when finished testing
    //return in_array( array_shift( _menu_get_active_trail()), $valid_top_levels);
    return TRUE;
    ?>
  5. Enable the checkbox 'Show if the following PHP code returns TRUE (PHP-mode, experts only).'
  6. Make the new block visible.
  7. When satisfied, uncomment the line as indicated in the above PHP code

theming

redtrafik - June 20, 2008 - 13:41

Thank you for this code. It is really helpfull.
When testing it, I realize that it would be better (for me) to use theme_menu_tree instead of menu_tree function as the first adds a "class=menu" to the root "ul" tag of the printed menu. This let you theme your block easier.

Again thx Drupal ;-)

Rafik

 
 

Drupal is a registered trademark of Dries Buytaert.