2

I'd like to replace the link title on the top link of my secondary navbar (called "explorer", links to "mysite.net/explorer", but actually just triggers a dropdown-menu) with a glyphicon and the current user's name.

According to this thread, a title callback is the way to do this. However, adding this code in my template.php has no effect at all.

function mythemename_menu_alter(&$items) {
  $items['explorer']['title callback'] = 'mythemename_title_callback';
}

function mythemename_title_callback($arg1, $arg2) {
  global $user;
  $title = '<span class="glyphicon glyphicon-user" aria-hidden="true"></span>'. $user->name;

  return $title;
}

What am i doing wrong? And why does the callback function take 2 parameters? (It doesn't work with 1 or 0 params.)

The "menu token" module almost crashed my site after installation, so I don't want to use that.

1
  • Is mysite.net/explorer added by a module, or does it point to a page you created, including a view?
    – avpaderno
    Commented Jan 24, 2015 at 17:57

2 Answers 2

0

One way is to use theme_menu_link(). Use debug() or Devel's dpm() to browse through $variables in order to get a grip on how the array is structured. Then locate the particular menu link in your code and theme it to your requirements. In the API documentation there are plenty of examples in the comment area that may suit your needs.

2
  • Theme functions should be used to alter the way an element is rendered, not to alter a link title. Eventually, the OP should use a preprocess function, but I think an alter hook should be used.
    – avpaderno
    Commented Jan 24, 2015 at 18:07
  • Sure. I should have said 'One quick but dirty way'.
    – Wtower
    Commented Jan 25, 2015 at 9:29
0

If you only want to change the title of an menu item, you should be able to do this through the administration. Go to admin/structure/menu and edit the correct item in your secondary menu.

To use hook_menu_alter() you must add it in a custom module not inside your template file. Remember to clear you cache in order for the changes to take effect. To clear the cache you can goto admin/config/development/performance.

4
  • Themes can implement alter hooks. The problem is that hook_menu_alter() is used to alter what hook_menu() returns, not to alter links added within the user interface.
    – avpaderno
    Commented Jan 24, 2015 at 17:29
  • @kiamlaluno - I tried adding it to my own template file and cleared my cache, but the hook never seemed to be called, so I assumed that one was forced to use a custom module for this. Commented Jan 24, 2015 at 17:30
  • See drupal_alter(), the code after the "Allow the theme to alter variables after the theme system has been initialized" comment. It is adding $theme_key . '_' . $hook to the $functions[$cid] array of the called functions.
    – avpaderno
    Commented Jan 24, 2015 at 17:36
  • @kiamlaluno - Strange I cant get it to work? Hm I must have made some kind of error. Commented Jan 24, 2015 at 17:38

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.