5

I'm trying to alter the text of a menu item dynamically based upon certain variables.

I'm using hook_menu_alter like this:

function mymodule_menu_alter(&$items) {
  $items['toggle-review-process']['title'] = 'asdf';
}

Problem is, it does nothing. That menu entry is in there, but it has no title normally, and adding the key isn't doing anything. How can I change the text of this menu item dynamically?

  • Have you cleared the cache? Menu entries are stored in the cache and after you add the hook, you have to clear the cache for the changes to take effect. – Dean Apr 18 '12 at 2:01
  • Yeah I did. Still no joy. – Joren Apr 18 '12 at 17:59
3

Be sure too understand how the menu system works. The menu structure is only build once every time the cache is cleared. It is at that moment that hook_menu_alter() will be called. The results are then stored in the database. So simply implementing this hook is not sufficient to create dynamic menu titles. You can use it though to add a title callback that will be called every time a link to the menu item will be rendered. For details have a look at http://drupal.org/node/140311 (D6, but basic logic still applies to D7).

From the top of my head:

function mymodule_menu_alter(&$items) {
    $items['toggle-review-process']['title callback'] = 'mymodule_title_callback';
    $items['toggle-review-process']['title arguments'] = array(1, 'some string');
}

function mymodule_title_callback($arg1, $arg2) {
    $title = 'create the title';
    return $title;
}
  • That is fine for modifying the title, but what if something else needs to be changed dynamically? Is there another, more generic approach which could help? – Shawn Jan 11 '13 at 21:21
  • I think this does not change the text of the menu link as it appears in the menu. It changes the title in the page, in the body and the header, and on other places. But not the menu link's text. Isn't that so ? – commonpike Dec 28 '13 at 20:22
  • No, that's not the case. The title callback, or the title key, whatever you use designates the menu title as it will be rendered. If not otherwise overridden this will also affect the page title. See api.drupal.org/api/drupal/modules%21system%21system.api.php/… for more details. – berliner Dec 29 '13 at 4:13
2

If it is a menu item that you defined in hook_menu() then you can simply add a title callback declaration and, if necessary, title arguments to pass to it.

If the menu item was created elsewhere, I've found that implementing hook_preprocess() allows you to alter the menu text through the mammoth $variables array.

This will only work for menu items on uncached pages; otherwise the first title displayed is cached along with the page.

1

To create / change dynamic menu you could use Menu Token module.

Menu Token module provides tokens, that could be used in title or in path of menu items (links). For example, if you create a menu item with path: "user/[current-user:uid]", the url will be changed "on fly" to: "user/1" (assuming you are user 1).

It has dependency on the Token module.

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.