One of many things I want to do in Drupal is to have

Hi: "User name" 

link for /users page. (In the future I also want to have a control over visibility of menu tabs and their links depending on the logged user.)

Currently I have a quick, easy and relaxing solution - hacked page.tpl.php which works perfectly. Unfortunately I it is not a perfect (nor profesional) solution because the website will brake down as soon as I update the drupal core.

Is there any way of injecting php code into menu link/title without messing up the core?

I've been thinking about using translation module, menu module, and others but so far I have not been successful. I know that menus in drupal are designed to be static, but there must be a walk-around.

I know that menu_token can solve some of my problems but it does not work for me (when installed I have memory allocation issues in php server or crashing MYSQL).

3 Answers 3


I finally figured this out. YES it is possible to have dynamic menus in Drupal.

You start by enabling dynamic access to menu item with the ['alter'] flag and hook_menu_link_alter(&$item).

function mycode_menu_link_alter(&$item) {
    if($item['menu_name'] == 'user-menu'){
        $item['options']['alter'] = TRUE;

Note that menu_link_alter works on a menu item (not the whole menu) and needs to be trigered by editing and saving that item (for example disable->save->enable->save->).

Once the menu item is activated, I can use hook_translated_menu_link_alter(&$item, $map)

function mycode_translated_menu_link_alter(&$item, $map){
    //drupal_set_message("Menu: ".$item['menu_name']." Item: ".$item['title']
    if($item['menu_name'] == 'user-menu'){
        global $user;
        $item['title'] = "Hi: $user->name";

Other very useful thing you can do is to disable the menu link

$item['hidden'] = TRUE;

or (my favorite) change the links

if($item['menu_name'] == 'main-menu'){
   switch ($item['title']){
       case "Home":
            $item['href'] = 'not/home/page';

There is Menu token

Menu Token module provides tokens, that could be used in title or in path of menu items (links). For example, if you create a menu item with path: "user/[current-user:uid]", the url will be changed "on fly" to: "user/1" (assuming you are user 1).


Have you hacked the page.tpl.php file which came installed with Drupal? I hope not. The way you do it (the Drupal way) is you place a copy of that file in your theme folder (/sites/all/themes/YOURTHEME/templates) instead, clear cache, and you're done - from now on Drupal will use your template. During any core upgrade you typically leave the sites folder in place and thus your hack, your theme, your everything stays intact.

As per PHP code in menu, I have seen that in OM Maximenu, but I haven't tested this particular feature.

As with any international site - it's advisable to first test any newly installed module against its i18n support before you test any other features.

  • Artur I am working on a developed web site with a hacked core. Because so far I did not manage to update the core without breaking the site I am redeveloping the hacks that were done and I want to do it properly (if possible). Thanks for your hints, I will have a look at that.
    – Pawel G
    Commented Dec 17, 2012 at 16:42

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.