0

This seems so simple, there -must- be a way, right?

I want to modify the title of a menu link dynamically. I've tried the techniques in this question:

Change menu link title dynamically?

...The last one ( using menu_token() module) seemed promising, but that threw an error when I tried to apply it to the menu item in question.

I also tried the technique here: https://www.drupal.org/node/1538094

...but that only modifies the Page title... not the link of the -menu-.

So I tried the following code, which doesn't appear to do anything (although I am sure that the variable is being assigned.)

Any other ideas? I must be missing something -obvious-, right?

function MY_THEME_preprocess_menu_link(&$variables) { if (isset($variables['element']['#original_link']['menu_name'])) { if($variables['element']['#original_link']['menu_name'] == 'secondary-navigation') { if($variables['element']['#title'] == 'Cart') { $new_title = 'Cart(4)' ; $variables['element']['#original_link']['link_title'] = $new_title; $variables['element']['#title'] = $new_title;

    }

}

} }

EDIT: I also tried this:

function MYMODULE_menu_link_alter(&$item) { // add in an if statement here to just target the links you want to change on the fly $item['options']['alter'] = TRUE; }

function MYMODULE_translated_menu_link_alter(&$item, $map) { if($item['mlid']==1166) { // the watchdog fires properly watchdog('test', '

' . print_r($item, 1) . '
');
$item['link_title'] = 'Cart(x)'; $item['title'] = 'Cart(x)';
} }

...and the above did not work either.

This has gotta be something obvious, right?

4
  • What went wrong with the other options that were listed in drupal.stackexchange.com/questions/28642/… ?
    – rjacobs
    Commented Mar 18, 2015 at 20:07
  • If you read the topic, you'll see. hook_menu_alter() is only updated after every cache clear. And as I wrote, the menu_token module seems to have a bug in D7.
    – jchwebdev
    Commented Mar 19, 2015 at 0:15
  • It seems like you are pretty close, so it's probably worth doing a little more debugging on the hook_menu[_link]_alter() options. The fact that those hooks don't fire when the link is rendered is not really an issue, as they are not doing the dynamic modification directly, their other complementary hooks/callbacks are doing that (and those do fire each time the link is rendered).
    – rjacobs
    Commented Mar 19, 2015 at 3:45
  • Perhaps you have a caching layer that needs to be cleared? If the rendered output of the menu is cached somewhere (page cache, block cache, etc.) that might be masking the change. That could also explain why your theme preprocess solution is not working either.
    – rjacobs
    Commented Mar 19, 2015 at 3:50

1 Answer 1

1

It looks like the other answer on that post you linked to is quite relevant. It references hook_menu_alter() as a tool to:

... add a title callback that will be called every time a link to the menu item will be rendered

However, I'm not sure if that solution would work for manually-created menu links items with custom title text. The other, possibly more universal, option that I can think of is using hook_menu_link_alter() along with hook_translated_menu_link_alter(). The former is used to "flag" a link as alterable (when the link is saved), and the latter is used to actually alter it (at render time).

Actually, it looks like this post covers both these options in even more detail, so I won't repeat any of their code samples here.

This distinction about which alter hooks fire at save/cache-clear time vs. render time is important as it often means that you have to leverage a hook + callback, or 2 hooks, to implement dynamic alterations. For example, if you choose the second option you use hook_menu_link_alter() to declare the link as alterable, and though that hook does not fire each time the link is rendered, it "registers" the fact that hook_translated_menu_link_alter() should in fact be called at render time for that specific link.

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.