How do I modify a menu link title dynamically? 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(&amp;$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(&amp;$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(&amp;$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?
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.