user1920216
user1920216

Reputation:

Modify the title given in hook_menu() from another module

How can I change the title given to the /user/[uid] page from Your account to Welcome [user name] for logged-in user, where [user name] is the username for the currently logged-in user?

Upvotes: 1

Views: 2523

Answers (5)

avpaderno
avpaderno

Reputation: 29679

In Drupal 7, hook_menu() and hook_menu_alter() are just invoked when the data about routes implemented from modules needs to be refreshed, for example when a module is enabled, disabled, installed, or uninstalled. An implementation of hook_menu_alter() that uses the name of the currently logged-in user in the title would show the same username for different users.
Differently, the title callback associated with a route is called every time the page associated with that route is rendered.

The correct code would be similar to the following one.

function mymodule_menu_alter(&$items) { 
  $items['user']['title callback'] = 'mymodule_user_profile_title';
}

function mymodule_user_profile_title() {
  global $user;
  return user_is_logged_in() ? t('Welcome, @name', array('@name' => format_username($user))) : t('User account');
}

Notice that the first argument of t() needs to be a literal string, not a dynamic value as in t($user->name) because the database table containing the string translations would not contain the translation for every username used in a site. It is also wrong because the shown username is invariant respect the language used on a site: For example, in an Italian site, a username like Albert isn't translated to Alberto, nor Vincent is translated to Vincenzo.

When showing a username in the UI, it is always preferable to use format_username(), which allows to third-party module to change what shown as username. (For example, a module could show the content of a user field, instead of showing the login username.)

If you aren't willing to write custom code, you could use the String Overrides module. If you don't want to use any module just for changing the title of the user profile page, you could add the following code in the settings.php file used for the site.

$conf['locale_custom_strings_en'][''] = array(
  'My account' => 'Welcome to my site'
);

Notice that, either using the String Overrides module or adding the $conf['locale_custom_strings_en'][''] array in the settings.php file, you cannot:

  • Provide a string that changes basing on the logged-in user (which is what the question is asking for)
  • Provide a string that is used only on specific pages

The latter case could be a pro or a con. If the string that needs to be changed is generic enough, it would be replaced even when it should not.

Upvotes: 0

Thomas4019
Thomas4019

Reputation: 573

The String Overrides module should make this easy.

Upvotes: 0

unor
unor

Reputation: 96597

You should be able to accomplish this with the Menu token module:

Menu Token module provides tokens, that could be used in title or in path of menu items (links).

(It requires the popular Token module.)

Upvotes: 1

Shaun
Shaun

Reputation: 520

Note that with Drupal 7.23, the user.module includes a 'title callback' to determine if the user is logged in or not, and respond with a corresponding title.

Code that worked for me (through theme template, instead of a custom module):

    function YOURTHEME_menu_alter(&$items) { 
      $items['user']['title callback'] = 'YOURTHEME_user_menu_title';
}

    function YOURTHEME_user_menu_title() {
      global $user;
      return user_is_logged_in() ? t($user->name) : t('User account');
}

Upvotes: 0

Muhammad Reda
Muhammad Reda

Reputation: 27023

Use hook_menu_alter.

Alter the data being saved to the {menu_router} table after hook_menu is invoked.

Code sample:

function MYMODULE_menu_alter()
{
    global $user;
    if($user->uid != 0)
        $items['user']['title'] = 'Welcome ' . $user->name;
}

Upvotes: 2

Related Questions