Advanced theming with functions

Last modified: March 24, 2009 - 13:41

The Nice menus module comes with several theme functions which you can use directly. This lets you have greater control over the HTML and avoids having the additional code that wraps a Nice menu when it is generated by the block system. This page will provide a brief overview on how to use a theme function directly, but you should refer to the Theming Guide for your version of Drupal for more detailed information on using theme functions. Details about the Nice menus functions themselves follow in the next sections, identified by version.

All theme functions can be either called directly, as is, from Nice menus or you can override them in your theme. They are all located in the nice_menus.module file and begin with theme_nice_menu. These examples below are only to show the method and show the simplest form of the function. For details on the parameters you can/should use, please see the version-specific documentation that follows.

Call a function directly in your theme's template file

Just print (or echo) this line:

<?php print theme('nice_menu_primary_links'); ?>

Or use the following version, which tests to see if Nice Menus is enabled before rendering the links:

<?php
if (in_array('nice_menus', module_list())) {
  print
theme_nice_menu_primary_links($primary_links);
}
else {
 
// Note: the exact syntax of this line may change,
  // depending on your theme. Look at how your theme
  // prints this line, and edit it accordingly.
 
print theme('links', $primary_links);
}
?>

The longer version is useful when there is a possibility that Nice Menus may be disabled or uninstalled, such as in multi-site installs, or during upgrades.

To override (change) the output

To change the look of the output, copy the entire function from nice_menus.module into your theme's template.php and rename the file to match your theme.

<?php
function THEMENAME_nice_menu_primary_links($direction = 'down', $menu = NULL) {
 
$pid = variable_get('menu_primary_menu', 0);
 
$output = theme('nice_menu', 'primary', $pid, $direction, $menu);
 
// Adding this silly line just to show that you can change the output of the function.
 
$output .= t('I can put anything I want in this function.');
  return
$output['content'];
}
?>

To exclude certain items from Nice Menus

Add the following code to your theme's template.php, changing YOURTHEMENAME to your theme's name:

<?php
function YOURTHEMENAME_nice_menu_tree($pid = 1, $menu = NULL) {
 
// For multiple items, edit the next line like this:
  // $menu_item_to_ignore = array(123, 456, 789);
 
$menu_item_to_ignore = array(123);
  if (
in_array($pid, $menu_item_to_ignore)) {
    return
'';
  }

  else {
    return
theme_nice_menu_tree($pid, $menu);
  }
}
?>

Correction in the code given above

sainzu - May 7, 2009 - 15:11

I just wanted to point out a small typo in the code that could throw newbies off.
The following line

<?php

...
$output .= t('I can put anything I want in this function.');
...
?>

should really be

<?php

...
$output['content'] .= t('I can put anything I want in this function.');
...
?>

otherwise you will get some unintended result.

Correction...

jeff00seattle - May 20, 2009 - 07:11

To the section Call a function directly in your theme's template file:, I believe it should be as follows, because theme_nice_menu_primary_links() is deprecated and it does not take parameter $primary_links:

<?php

if (in_array('nice_menus', module_list())) {
  print
theme_nice_menus_primary_links();
}
else {
  print
theme('links', $primary_links);
}             
?>

Jeff in Seattle

... correction ...

Domsou - September 14, 2009 - 15:13

There is a typo in the function name : the nice_menu part does not end with a 's' :

<?php
if (in_array('nice_menus', module_list())) {
  print
theme_nice_menu_primary_links();
...
?>

 
 

Drupal is a registered trademark of Dries Buytaert.