Home

Tag // php

Customizing Drupal Menus

As the interface for your website, the menu gets plenty of action. You might want to tune it up a bit, adding something to make it special. Drupal has an extensive API for interacting with the menu items you create.

On our website, we wanted subtext for each primary menu item – as you see above. Drupal usually displays the link titles only. There is a description field for each menu item, and we thought that would be a great place to enter the subtext we enter. I set out to generate my own menu and place it in a block on my page. menu_navigation_links() looked like a promising function from the menu API.

I enabled the devel module and put the execute PHP block at the top of the page. I got to work creating the PHP that would do the trick. Dealing with arrays can be tricky if you don’t do it often. Using the PHP function print_r() , you can view the contents of your menu array. Like so:

< ?php
$menuitems = menu_navigation_links('primary-links', $level = 0);
print_r($menuitems);
?>

Once you have the PHP worked out, make sure the PHP filter module is enabled for the site under admin/build/modules. Create a block at admin/build/block. Insert PHP code and change the input type to PHP.

< ?php
//Get the menu structure and place in a variable. An array is returned.
$menuitems = menu_navigation_links('primary-links', $level = 0);

//Loop through the menu items, setting two classes for the design and outputting what we want
foreach ($menuitems as $key=>$value)
{
echo '
';
echo ''.$menuitems[$key]['title'].'';
echo ' 
';
echo $menuitems[$key]['attributes']['title'];
echo ' 
';
echo '
';
}

?>

More information on the Drupal menu API for all versions:

http://api.drupal.org/api/group/menu/6

Comments Off on Customizing Drupal Menus

Aw, we like you too!

© 2017 Little Bird LLC » Email