6

I modifed my main menu using the following code, but I need the <span class="description"> elements only for the first level menu items, not for the sub-menu items.

How can I achieve this?

function neofelis_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';
  $element['#localized_options']['html'] = TRUE;

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  
  if ($element['#original_link']['menu_name'] == "main-menu" && isset($element['#localized_options']['attributes']['title'])) {
    $element['#title'] .= '<span class="description">' . $element['#localized_options']['attributes']['title'] . '</span>';
  }
  
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

1 Answer 1

1

Looks like your friend is: $element['#original_link']['depth'];

You would adjust your conditional to be something like:

 // Not sure if top level depth is 0 or 1 for this so experiment with that a bit.
 if ($element['#original_link']['menu_name'] == "main-menu" && 
     isset($element['#localized_options']['attributes']['title']) &&
     $element['#original_link']['depth'] == 0
 ){
    $element['#title'] .= '<span class="description">' . $element['#localized_options']['attributes']['title'] . '</span>';
 }

Check out this comment from the drupal documentation.

Your Answer

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

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