By Joren


2012-04-18 01:24:00 8 Comments

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?

3 comments

@user12068 2012-12-17 09:11:30

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.

@berliner 2012-12-16 17:03:08

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;
}

@Shawn 2013-01-11 21:21:38

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?

@commonpike 2013-12-28 20:22:58

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 ?

@berliner 2013-12-29 04:13:04

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.

@scronide 2012-11-14 00:58:16

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.

Related Questions

Sponsored Content

1 Answered Questions

How do I dynamically change the title of a menu link?

  • 2015-02-14 18:52:32
  • Jason
  • 756 View
  • 0 Score
  • 1 Answer
  • Tags:   routes

4 Answered Questions

[SOLVED] Advanced menu theming in Drupal 7

  • 2012-03-07 04:46:14
  • Lester Peabody
  • 23177 View
  • 15 Score
  • 4 Answer
  • Tags:   7 routes theming

2 Answered Questions

[SOLVED] Best way to go about my news menu item

1 Answered Questions

[SOLVED] Change Menu Link Title Dynamically

  • 2016-04-08 14:29:06
  • Max
  • 584 View
  • 0 Score
  • 1 Answer
  • Tags:   7 navigation

2 Answered Questions

Ordering results for D7 form autocomplete fields

  • 2014-02-12 09:53:29
  • Matt Fletcher
  • 322 View
  • 1 Score
  • 2 Answer
  • Tags:   7 ajax json

1 Answered Questions

[SOLVED] How do I modify a menu link title dynamically?

  • 2015-03-18 17:54:29
  • jchwebdev
  • 661 View
  • 0 Score
  • 1 Answer
  • Tags:   routes

2 Answered Questions

[SOLVED] Howto limit access to a main menu link using drupal 7 api

  • 2014-04-17 15:07:01
  • user2181910
  • 398 View
  • 0 Score
  • 2 Answer
  • Tags:   routes

1 Answered Questions

[SOLVED] Drupal 7: How to create dynamic link based upon url

  • 2012-03-20 18:16:02
  • ruess
  • 1836 View
  • 0 Score
  • 1 Answer
  • Tags:   routes

0 Answered Questions

Commerce: Change cart line item dynamically on checkout page

  • 2012-08-24 06:17:24
  • infojunkie
  • 1584 View
  • 1 Score
  • 0 Answer
  • Tags:   7 commerce

1 Answered Questions

[SOLVED] How to dynamically change the title of a menu item

  • 2012-05-20 05:44:50
  • uwe
  • 4523 View
  • 1 Score
  • 1 Answer
  • Tags:   7 routes

Sponsored Content