1

I want to create link to user from the menu created by UI. I tried with hook_menu but the link is 'user', but it doesn't work, because don't want to create new page. I would like to alter title or add menu item programmicaly to use First name in the the menu title.

What i know is the 'mlid' = '3228' and menu name is 'menu-user-links-top'

PS. I also tried with hook_menu_link_alter but it doesn't work, for all users (the first name appears after pressing edit menu and uses only admin's first name).

5
  • are you want to change the menu-item to user-name?, So whenever user logged in the menu-item(its title) gets change to registered username.
    – CodeNext
    Commented Sep 6, 2016 at 8:32
  • yes, for the user first name, actually, but it doesn't matter really. I need to find way to create menu item to user page without hook_menu or hook_menu_link_alter becuase it doesn't work. I noticed that I can use theme funtions, but I would preffer my own module.
    – user63492
    Commented Sep 7, 2016 at 6:46
  • but how are you providing first-name to drupal site? Drupal has only one thing Username that is a one string like you can see in Stackexchange site. Does your site have provision to provide first-name and last-name separately when user creates his/her account?
    – CodeNext
    Commented Sep 7, 2016 at 6:57
  • did you get my point in previous comment, we can fetch the user-name based on the uid, and can show it in-place of menu-item as that variable is already there in Drupal. But, there is nothing called first-name and last-name part of that username variable in drupal, as far as I know. If there is, I am not aware presently.
    – CodeNext
    Commented Sep 7, 2016 at 7:10
  • Hi, I don't see problem with user-name or account fields. I know to access them, the problem is how to create PHP menu item title.
    – user63492
    Commented Sep 7, 2016 at 8:24

1 Answer 1

2

For example, if I take the User-Menu in bartik theme, and I want to change the My Account menu-title with Username, So whenever, the user logged in, instead of My Account that menu-item shown the Username.

Image-1: Default menu-item

enter image description here

Image-2: After the code

enter image description here

function YOURTHEMENAME_translated_menu_link_alter(&$item, $map) {
    global $user;
    if ($item['href'] == 'user') {
      $item['title'] = t($user->name);
    }
}

Now, same way as shown in the above code, you can change the apply the href of any particular menu-item and there it will show the user-name instead of the menu-title.

I am not sure are you looking for this or other, but this is the straight code to change the men-item with username.

2
  • Question answered :) I can also use this hook in module. Thank you
    – user63492
    Commented Sep 7, 2016 at 9:32
  • Generally you can put in template.php file as it is very small code, but if you want you can put in module also. But remember to find the correct href of the menu-item which you are targeting. You can use devel module for that.
    – CodeNext
    Commented Sep 7, 2016 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.