0

I made a menu in administration, structures, menu. I want to change the title, as it appears on the menu, of one of the links based on the gender of the currently logged in user.

I have found several answers to similar questions but I can't figure out how to implement the solutions. Some of them suggest using hook_menu_alter(&$items) and a separate callback function, but I don't understand the structure of $items. I was hoping to just print out the value of $items, find the string I wanted to change, and change that value in $items in the hook. Apparently, though, you can't debug the $items variable because hook_menu_alter is rarely called. I tried to figure out the structure of $items from a bunch of other questions and answers and I found stuff like this:

$items['user/%user_category/edit']['title']

Why is 'title' a property of a path? Does this mean that every menu on your site that has a link to that path will display the title that you set the above variable to? What if you wanted a link to a specific path on one menu to have a different label than a link to that path on another menu? Isn't there somewhere I can access and change the title of a menu link like this

$allMenuData['My Custom Menu']['an item on my menu']['title']
11
  • 1
    This has already been asked/solved in the past: drupal.stackexchange.com/questions/28642/… Commented Feb 14, 2015 at 19:07
  • I just tried that solution again, for good measure. I have a menu link title of "Site Users" and it links to a page called "main-profiles". I made a function called alter_callback() that returns a string. In the hook_menu_alter, I tried $items['Site Users']['title callback'] = "alter_callback" and I tried $items['main-profiles']['title callback'] = "alter_callback" and neither of them have any effect on the menu link title. What am I doing wrong?
    – Jason
    Commented Feb 14, 2015 at 20:14
  • 1
    have you cleared your cache? Commented Feb 14, 2015 at 20:18
  • Yes. For both tests, I cleared the cache, disabled the module, re-enabled the module and cleared the cache again.
    – Jason
    Commented Feb 14, 2015 at 20:19
  • Oké I suggest debugging the code with Xdebug to see what is/isn't running. You could also do this with dsm's of course. Commented Feb 14, 2015 at 20:21

1 Answer 1

0

You might try hook_preprocess_menu_link()

function MYTHEME_preprocess_menu_link(&$variables) {
    global $user;

    if (isset($variables['element']['#original_link']['menu_name'])
        && $variables['element']['#original_link']['menu_name'] == 'my-menu-machine-name'
        && $variables['element']['#title'] == 'ORIGINAL LINK TITLE') {

        $new_title = (check for gender) ? 'New title 1' : 'New title 2';
        $variables['element']['#title'] = $new_title;
    }
}

Fill in your menu machine name, the title you want to change, the conditional for the gender and the new titles depending on the gender value.

3
  • That didn't work, so I tried to var_dump($variables['element]['#main-profiles']) from that hook and it says #main-profiles is an undefined index. main-profiles is definitely the link in my menu.
    – Jason
    Commented Feb 14, 2015 at 23:40
  • the only spots you should need to change are 'my-menu-machine-name', 'ORIGINAL LINK TITLE', the conditional, and 'New Title 1', 'New Title 2'. the #original_link key should not be changed.
    – thumbson
    Commented Feb 14, 2015 at 23:41
  • It worked! Thank you. Also, I had some luck with hook_menu_alter hook. I'll post details in a comment above.
    – Jason
    Commented Feb 15, 2015 at 0:07

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.