0

I need to put an id in menu links i have this code from here https://api.drupal.org/api/drupal/includes!menu.inc/function/theme_menu_link/7.

function theme_menu_link(array $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";
} 

And have found the code i need but am struggling where to place it in the function. This code https://api.drupal.org/comment/45408#comment-45408:

l( t('Refresh Page'), $path . '/getdata', array(
              'attributes' => array(
                'id' => '', 
                'class' => 'btn-small btn-primary '
              ),'query' => drupal_get_destination() , 'html' => TRUE)),

    );

Where would it go, i know what i need, but i am having difficulty doing it.

Thanks

1 Answer 1

1

You can use this module to place different menu attributes to your menu links:

Menu Attributes

This simple module allows you to specify some additional attributes for menu items such as id, name, class, style, and rel.

You should use this module when

  • You want to "nofollow" certain menu items to sculpt the flow of PageRank through your site.

  • You want to give a menu item an ID so you can easily select it using jQuery.

  • You want to add additional classes or styles to a menu item.

You can also add the ID using theme_menu_link like you were using above, you would place that code in your template.php of your theme.

If you use the Devel module you can print out the $variables array and see the different parts of the array there. You would basically want to use the mlid and the attributes part of the link to build out the ID (see code snippet below).

$element['#attributes']['id'] = 'menu-' . $element['#original_link']['mlid'];

If you need any other information just let me know, thanks!

7
  • thank this is a real help. But all the themes are already set up i just need to add that bit of code to menu_link. But don't know where, this is a help though.
    – jelly46
    Nov 8, 2015 at 11:13
  • 1
    The menu attribute is just a module, module readme would have instructions for that most of the time. The code part if you choose that path would go info your themes template.php (and replace theme_menu_link with YOURTHEMENAME_menu_link). Maybe that will help. Nov 8, 2015 at 13:30
  • No that won't help me, but thanks
    – jelly46
    Nov 8, 2015 at 14:17
  • 1
    Are you meaning what line to put the code on? Nov 8, 2015 at 17:58
  • 1
    So you have a different Id for every link but you want the side menu (probably secondary menu) to have an id of something like secondary-menu-22 ? Nov 12, 2015 at 21:44

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.