Add a custom menu to the header or footer of a views display (page or block)

Last modified: November 10, 2009 - 01:47

So- you have a view created, and you have a block or page display for the view. But now you want to add a few custom links, and you *don't* want to hard code them. Why do you want Drupal to handle them? Some advantages are that Drupal can recognize when a link is 'active' and add the proper CSS for the link; and it can also recognize if Drupal is installed in a sub directory and have a base_url in your settings.php and it will render the links properly.

This how-to assumes you have the view already created and that you have the custom menu. Visit your admin/build/menu/add and admin/build/views/add if you need to catch up.

  1. Enable PHP filtering.
    • The text field for header or footer allows filtered HTML and full HTML by default, but you must enable PHP input filtering to allow code in a view. In Drupal 6, this is disabled by default. Find it under admin/build/modules listed under 'core'. Tick [x] PHP filter, and scroll down to save configuration.
  2. Now add the code to display the menu in the view
    • Edit your view, admin/build/view...
    • Look in the 2nd column Under "Basic Settings" click - Footer: None
    • Scroll down to the text field below "Defaults: Footer", and paste in this code:
                <?php print theme('menu_tree', menu_tree('menu-name')); ?>
             
    • Where is says 'menu-name' - change name to your menu name. For example 'menu-addimage', where addimage was the name of the menu.
    • Note: When you made the custom menu, Drupal appended "menu-" to the name, so look at the path in the admin/build/menu area to double check the name.
    • Under, click Input format, select "PHP code".
    • Click update.
    • Then save. You should see your menu now.

FYI: API reference in case someone is searching: http://api.drupal.org/api/function/menu_tree/6
function menu_tree

Credits: Thanks to NikLP and JohnAlbin who helped sort this out!

While this approach worked

ghazlewood - July 24, 2009 - 12:19

While this approach worked for me in theory looking at the output of the page I was getting a double ul which was messing things up. e.g <ul class="menu"><ul class="menu>

Using the below snippet from http://agaric.com/note/printing-arbitrary-menu-just-primary-links-and-se... fixed the problem

<?php
$menu
= menu_navigation_links("menu-name-here");
print
theme('links', $menu);
?>

Hope this helps someone else!

 
 

Drupal is a registered trademark of Dries Buytaert.