0

I'm having a theming problem with my Drupal secondary navigation. I have a two level menu set up - i.e. five parent links, two of whom have children. The problem I'm seeing is that the children have the same CSS classes and IDs. Consequently, when I click on a parent that has children, they are appearing the same as the parents.

I have tried to modify the menu via my template.php and page.tpl.php to no avail. I have the following in my template.php file and it is throwing errors saying $sub_menu does not exist:

function framework_menu_link(array $variables) {

  unset($variables['element']['#attributes']['class']);
  $element = $variables['element'];
if($variables['element']['#attributes'])
$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 . "</li>\n";
}

Any help or guidance anyone could provide would be appreciated.

Thank you!

+Chris

1

Have you tried the Menu Attributes module? It's easy to use and has great features:

The module currently allows you to set the following attributes for each menu item:

  • Id
  • Name
  • Target
  • Rel
  • Class
  • Style
  • Accesskey
0

Revised answer. First, there are some issues with the code

  1. $sub_menu isn't set because you have it within an if statement, which doesn't seem necessary.

  2. With your current code, $sub_menu will never be rendered, because it is no longer in the return statement

  3. You are removing all classes from every menu link, not just the sub-menu items with:

    unset($variables['element']['#attributes']['class']);

You can use the following to add .has-sub-menu to any li that has a sub menu, which will save you, maybe, one level of nesting. Alternatively, you could just use css to target the sub-menu .main-navigation ul li ul is your sub-menu most likely.

function mid_america_trucking_show_menu_link(&$variables) {     
  $element = $variables['element'];
  $sub_menu = '';
  if ($element['#below'] && !empty($element['#below'])) {
    // this link has children
   if (isset($element['#attributes']['class']) && !empty($element['#attributes']['class'])) {
      array_push($element['#attributes']['class'], 'has-sub-menu');
    }
    else {
      $element['#attributes']['class'] = 'has-sub-menu' ;
    }
    $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";

}  
  • My intention was to strip out the sub menu styles, yes. My goal is to then style the sub menu differently by applying custom class names. Where I'm getting confused though is where/how to apply those names. I believe the if variable element attribute check might not be triggering because as soon as I had $sub_menu in my return line at the bottom, Drupal complained saying $sub_menu was not a valid variable. And since I'm missing the opening { that might explain it! – ccorbett Jun 24 '13 at 18:16
  • That did not work. I closed off the { and I'm still getting Notice: Undefined variable: sub_menu in framework_menu_link() repeated over and over. Does that mean that if($variables['element']['#attributes']) check is failing? – ccorbett Jun 24 '13 at 18:25
  • See revised answer. Why are you checking if #attributes is set? – Chris Rockwell Jun 24 '13 at 18:46
  • I see now that a couple things in there were most likely unnecessary. Thank you Chris, for your time. I will try out your example and see how that goes. – ccorbett Jun 24 '13 at 19:16
  • No problem. Let me know if you must have a class on the sub-menu anchor tags, but I would recommend the CSS route. – Chris Rockwell Jun 24 '13 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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