Community

Adding menu link class to parent level links only (solved)

Edit: have it working now thanks to gausarts, below is the final function in case anyone is looking to accomplish the same thing.

<?php
function blocks_menu_link(array $variables) {
 
$element = $variables['element'];
  static
$item_id = 0;
 
  if (
$element['#original_link']['depth'] == 1) {
   
$element['#attributes']['class'][] = 'menu_' . (++$item_id);
  }
 
 
$sub_menu = $element['#below'] ? drupal_render($element['#below']) : '';
 
$output = l($element['#title'], $element['#href'], $element['#localized_options']);

  return
'<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>';
?>

Original post:

I'm trying to add an incremental class (menu_1, menu_2 etc) class to the parent list items in a theme's main menu. I'm working with the following function:

<?php
function blocks_menu_link(array $variables) {
 
$element = $variables['element'];
  static
$item_id = 0;
 
$element['#attributes']['class'][] = 'menu_' . (++$item_id);
 
$sub_menu = $element['#below'] ? drupal_render($element['#below']) : '';
 
$output = l($element['#title'], $element['#href'], $element['#localized_options']);

  return
'<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . '</li>';
}
?>

The function works, but I'm trying to modify it to only add the class to the parent items and not count the rest. Here's a rough example of what I'm after:

<ul>
  <li class="menu_1"> Link 1 </li>
  <li class="menu_2"> Link 2
    <ul>
      <li> Sub Link 1 </li>
      <li> Sub Link 2 </li>
    </ul>
  </li>
  <li class="menu_3"> Link 3 </li>
  <li class="menu_4"> Link 4 </li>
</ul>

Any help would be greatly appreciated.

Comments

menu_link contains

menu_link contains information about depth. Try printing the $variables to see the structure.
Then you can use that info to limit the class to the depth 1, something like:

if ($element['#original_link']['depth'] == 1) {
  // do sumthing
}

love, light n laughter

Thanks for the suggestion but

Thanks for the suggestion but unfortunately it doesn't seem that's going to help in my case. Single menu items with no child links are depth 1, but parent AND child links are depth 2.

Did you put your class

Did you put your class inside, e.g.:

if ($element['#original_link']['depth'] == 1) {
  static $item_id = 0;
  $element['#attributes']['class'][] = 'menu_' . (++$item_id);
}

It works like you are after.

love, light n laughter

Hey that actually worked! I

Hey that actually worked! I didn't think it would because it seemed that menu items with child links were depth 2 but that worked like a charm. Thank you very much!

Great. Try adding a check to

Great. Try adding a check to limit the count to main menu, as well, something like:

<?php
$menu_name
= $element['#original_link']['menu_name'];

if (
$element['#original_link']['depth'] == 1 && $menu_name == variable_get('menu_main_links_source', 'main-menu')) {
  static
$item_id = 0;
 
$element['#attributes']['class'][] = 'menu_' . (++$item_id);
}
?>

For just in case you don't want the rest menus use the counter.

love, light n laughter