2

I'm using Drupal 7 and I want to append a unique ID to specific menus. My search result gives me the way to change the item of the menu, but I want to change the menu itself. Is there a way to do that?

1

4 Answers 4

0

Quick and dirty way:

Go to your themes directory and open up page.tpl.php. I'm using a Zen based theme and I have the following code which will print the secondary menu:

     <?php print theme('links__system_secondary_menu', array(
       'links' => $secondary_menu,
       'attributes' => array(
       'id' => 'secondary-menu',
       'class' => array('links', 'clearfix'),
      ),
      'heading' => array(
        'text' => $secondary_menu_heading,
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
    )); ?>

That can simply be edited to (modified line has asterisks):

     <?php print theme('links__system_secondary_menu', array(
       'links' => $secondary_menu,
       'attributes' => array(
       ***'id' => array('secondary-menu', 'my-new-id'),***
       'class' => array('links', 'clearfix'),
      ),
      'heading' => array(
        'text' => $secondary_menu_heading,
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
    )); ?>

Edit:

I don't really understand you when you say you're having trouble with the multi-dimensional menus and languages, but if you need more control, why not use theme_menu_tree? Override that in your theme's template.tpl.php with something like:

function my_theme_name_menu_tree($variables) {
  if ($variables['some_condition_you_care_about'])
    return '<ul class="menu" id="my_custom_id">' . $variables['tree'] . '</ul>';
  else
    return '<ul class="menu">' . $variables['tree'] . '</ul>';
}
2
  • i think your code doing same as print theme('links', array('links' => menu_navigation_links('primary-links'), 'attributes' => array('id'=> array('myid')) )); but the problem is the menu is multi dimensional and is in 2 languages and this function does not get the menu translated or with multi dimensions Apr 13, 2011 at 13:41
  • by the way what is the function that provide $links ? Apr 13, 2011 at 17:32
1

You can use the menu attributes module to add custom classes, id's, names etc... to any menu item in Drupal. Very handy module when you are looking to alter Drupal's menu system to create a custom look.

2
  • yes i know that module well , but it gives attributes to the menu items not the menu itself i need to assign an id to the basic menu. Apr 13, 2011 at 10:55
  • sorry i miss read it as menu item. you can use theme_menu_tree()
    – Jepedo
    Apr 13, 2011 at 10:59
1

As an alternative solution maybe you'd consider using the Menu block module. You use it to create blocks for specific menus, with custom depth ranges, starting item etc.

I think I use it for every menu in my site now as I love having that control AND as an added bonus it will add your unique IDs to each menu.

0

In order to theme the unordered list, you need to call theme_menu_tree(). You can edit your template.php in order to call this.

function THEMENAME_menu_tree__MENUNAME($variables){
  return '<ul class="your-custom-class" id="your-custom-id">' . $variables['tree'] . '</ul>';
}

Then, if you want to theme your links, call theme_menu_link().

function THEMENAME_menu_link__MENUNAME($variables) {
  $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

Now, the problem with Drupal 7 is that there is a major bug when it comes to displaying the active trail of custom menus.

See issue
Issue #520106 - No way to dynamically set active trail
Issue #942782 - Custom menus never receive an active trail

You are basically out of luck if you want some true custom menus. These issues are very long standing in the drupal community so I am not sure if they will be fixed anytime soon. I don't know if there is some PHP that can help with it. You can turn to some alternative modules in order to help ease functionaltiy, like Menu Attributes and Menu Block. They both can help get around the cruddy menu system in Drupal 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.