I needed a way to display submenus on pages on a website built in Drupal 6. The menu system itself, built with Drupal Menu’s primary links, was three levels deep. So, a small snippet of the menu as an example would be:

About Us >
    Directors
    History
    Mission
Services >
    Pricing
    Administration
        Trusts
        Estates
    Special Needs

If someone is on the “History” page, I want a sub menu to appear on the left with all the other links that appear under “About”. Drupal has a module called Submenu Tree which comes with hardly any instructions, and didn’t seem to work. So after much digging around, I found a quick and simple solution using some Drupal functions and the template.php file.

In my theme’s template.php file I created a new function called Submenu() that looks like this:

<?php

function Submenu() {

$menu_theme = menu_navigation_links(“primary-links”,2);
$menu .= “<ul>”;

foreach ($menu_theme as $value) {
  $menu .= “<li>”;
  $menu .= l($value[‘title’],$value[‘href’]);
  $menu .= “</li>”;
}

$menu .= “</ul>”;
return $menu;
}

Then, in your page.tpl.php file, whereever you want the menus to appear, just do a print(Submenu()); and voi-la!!

The code explained:

$menu_theme = menu_navigation_links(“primary-links”,2);

This is a drupal function that will return an array of all the links relative to the current menu, the second argument, the ‘2’, says I want to see all the links for the 3rd level. If I was on the “Trusts” page, this would return an array with “Trusts” and “Estate” link information.

$menu .= l($value[‘title’],$value[‘href’]);

This is another Drupal function that creates an automatic link based off link text and a path URL. Both of these are made available to us from the array we got in $menu_theme.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *