Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

How can I make my main menu link text dynamic?

My solution:

I tried to use Felix's answer, below, but it would not work for me. Obviously, the method suggested by Felix has worked for others and it is used in core (user.module, for example). However, I was able to conditionally alter my main menu link text at the theme level. I added this to template.php:

/**
 * Conditionally alters preprocess links variables.
 */
function MY_THEME_preprocess_links(&$variables) {
  if (MY_FUNCTION()) {
    if (isset($variables['links']['menu-562 active-trail']['title'])) {
      $variables['links']['menu-562 active-trail']['title'] = 'New blog';
    } elseif (isset($variables['links']['menu-562']['title'])) {
      $variables['links']['menu-562']['title'] = 'New blog';
    }
    if (isset($variables['links']['menu-563 active-trail']['title'])) {
      $variables['links']['menu-563 active-trail']['title'] = 'New notes';
    } elseif (isset($variables['links']['menu-563']['title'])) {
      $variables['links']['menu-563']['title'] = 'New notes';
    }
  }
}
share|improve this question
add comment

2 Answers

Drupal menu's are cached and this makes it hard to change then on the fly when the page is rendered. It is possible though using the functions aimed at translating menu items: see hook_translated_menu_link_alter.

This hook is called before every menu item is rendered IF it has the property ['options']['alter'] = TRUE.

You can set this property to menu items using hook_menu_link_alter.

Example code would be:

function MY_MODULE_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 MY_MODULE_translated_menu_link_alter(&$item, $map) {
    if($item['mlid']==89) {
        // this line is actually to change the link however if you inspect the $item
        // variable then you will be able to see the other property that you can change.
        $item['link_path'] .= 'my-new-path';
    }
}
share|improve this answer
    
Thanks. I've added the new code (appended to the base of my question). Cleared the cache, but nothing. Do I need to rebuild the menu_links table some how? –  24ma13wg Feb 14 at 20:21
    
After inspecting the 'options' column in {menu_links} the value remains the same: [BLOB - 6 Bytes]. So it seems that perhaps the menu link record is not being updated with ['options']['alter'] = TRUE; –  24ma13wg Feb 14 at 20:52
    
By resaving my main menu links in the menu UI I believe this called hook_menu_link_alter and set the 'options' flags. However, my menu titles still aren't changing on the fly. –  24ma13wg Feb 15 at 11:13
    
Yes, the first hook you use is hook_menu_link_alter. If you put a dmp call in hook_translated_menu_link_alter alter then does it get called? You need to dmp the item (or use print_r if you don't have devel installed) and check what options are there to see exactly what needs updating. There are some more example uses of the function on this SO post: drupal.stackexchange.com/questions/32013/… –  Felix Eve Feb 18 at 9:18
add comment

If you want to change the menu link text for an existing menu item, you could also use

hook_menu_alter

and add a different 'title callback'

e.g

/**
 * Implements hook_menu_alter
 */
function MODULE_menu_alter(&items){
  $items['node/add/blog']['title callback'] = 'MODULE_blog_title_callback';
}

/**
 * hook_menu title callback
 */
function MODULE_blog_title_callback() {
  if ($some_condition) {
    $title = "New blog post";
  }
  else {
    $title = "New alien lifeform";
  }
  return $title;
}
share|improve this answer
    
I don't think I can. See first bullet point of accepted answer here: drupal.stackexchange.com/questions/32013/… –  24ma13wg Feb 15 at 10:48
    
Ah yes, it depends if it's a hook_menu defined menu item or one added via the UI. Alternatively you could use a preprocess function for the relevant theme function. In D7 all theme functions actually have preprocess functions as well.. –  David Thomas Feb 15 at 10:49
    
I added my links through the menu UI because they appear in the main menu on every page. I believe the first answer should work. I've managed to set the 'options' flag on the two menu items I want, but my link titles still don't change on the fly. –  24ma13wg Feb 15 at 11:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.