Drupal 7: Modifying menu HTML output?
Asked Answered
B

1

17

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?

Binky answered 4/3, 2011 at 3:25 Comment(0)
I
18

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).

Instrumentation answered 5/3, 2011 at 0:40 Comment(2)
The accepted answer uses theme_menu_item which is Drupal 6. The equivalent in Drupal 7 is theme_menu_item api.drupal.org/api/drupal/includes--menu.inc/function/…Madi
@Madi Drupal 7 uses theme_menu_link instead of theme_menu_item.Telephoto

© 2022 - 2024 — McMap. All rights reserved.