0

Ive used the theme override below to add a span to the end of the li of my menu links. This is working but I need it so the span is only added to the active-train li.

function MYTHEMENAME_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 . ' <span>Something</span> ' . "</li>\n";
}
0

You can check if $element['#localized_options']['attributes']['class'] contain active-trail class and create your custom span based on this information.

function MYTHEMENAME_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';
  $span = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }

  if (isset($element['#localized_options']['attributes']['class'])) {
    if (in_array('active-trail', $element['#localized_options']['attributes']['class'])) {
      $span = '<span>Something</span>';
    }
  }

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

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.