0

I have a menu created through admin/structure/menu.

On some of the menus, I would like to place a var after the title.

I have tried placing the following in my theme template, but nothing happens.

 function mytheme_menu_link(array $variables) {
   print_r($variables['element']);
 }

How could I just place the content of a PHP variable after the menu link title?

1 Answer 1

2

If you just need to add a string after the menu title, you can use the following code.

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

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

  $output = l($element['#title'] . ' ' . t('the string you need to add'), $element['#href'], $element['#localized_options']);

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

The alternative would be using the following code, which would give to who translate the string more context to translate it.

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

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

  $output = l(t('!title the string you need to add', array('!title' => $element['#title'])), $element['#href'], $element['#localized_options']);

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

You could also implement a preprocess function to alter the title.

function mytheme_preprocess_menu_link(array $variables) {
  $variables['element']['#title'] = t('!title the string you need to add', array('!title' => $variables['element']['#title']));
}
1
  • couldnt get this to work on any menus. some links are created through modules, some through views, some through the menu form in drupal. I have added the code to my template and changed mytheme to my theme name and nothing changes. Also where do I specify which menu items?
    – Sam Healey
    Commented Oct 6, 2013 at 10:40

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.