Drupal 7: Modifying menu HTML output?

Donnie Thomas picture Donnie Thomas · Mar 4, 2011 · Viewed 45.5k times · Source

I am trying to modify the HTML output in a Drupal 7 theme that I am creating.

Basically, instead of the < li >s containing just plain < a >s with text, I want to include some additional HTML inside the < a >.

I know that it's possible to modify the HTML created by the menus in Drupal. I can see the following call in page.tpl.php:

<?php print theme('links__system_main_menu', array(
      'links' => $main_menu,
      'attributes' => array(
        'id' => 'main-menu',
        'class' => array('links', 'clearfix'),
      ),
      'heading' => array(
        'text' => t(''),
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
    )); ?>

which apparently calls the theme function, which creates the output. One way to modify the output would be to modify the theme_links function in theme.inc, right?

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links

I also know that you can put a hook in template.php to override the function which creates the HTML. I can't figure out how to create the actual override function. Can somebody point me in the right direction, please?

Answer

wildpeaks picture wildpeaks · Mar 5, 2011

What you would do is implement a hook to modify the output, not modify the "theme.inc" file directly.

For example, the accepted answer on this page: Drupal Override Custom Menu Template


And as a general rule, when you want to modify the output of something, either implement a hook (in a module or in the template.php of the active theme) or use a template with a predefined file name when such a case exists (when no template already exists, you can also modify the list of template suggestions using a module or the theme).