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?
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).