I want to display the uid (read-only) of the user on both the user/x and user/x/edit page.

By using the function below I managed to show the uid on the user/x/edit page.

function my_module_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
    if (arg(0) == 'user') {
        $form['uid_read_only'] = array(
            '#markup' => 'User id : ' . '<b>' . arg(1) . '</b>',
            '#weight' => -500,
            '#disabled' => TRUE,

How do I show the uid on the user/x page (view state)?

  • Your code will display the uid of the currently logged in user. This is only the same as x if the user edits his own account. If I edit the account of a different user, my uid will still be displayed instead of the uid of the user whose account I edit. I don't think this is what you want. – Oswald Sep 19 '12 at 13:27
  • You're right, thanks for noticing. I think just grabbing the arg(1) is sufficient, agree? – undersound Sep 20 '12 at 16:01
  • Yes, arg(1) is sufficient in your case. The alternative would be to call menu_get_object(), passing 'user' as $type and 1 as $position. This gives you the user object associated with the URL. But I do not see the benefits of this method. – Oswald Sep 20 '12 at 17:49
  • ok, to close it off...is it correct when I'm saying that the check I'm performing if (arg(0) == 'user') is not really necessary because this function form_user_profile_form_alter will always and only be executed on a page with arg(0) as user? – undersound Sep 20 '12 at 18:59
  • Taking only the Drupal core into account, you are correct: the check for arg(0) == 'user' is not necessary. Contrib modules might see this differently. I usually make a clear distinction between form builder functions (that don't make any assumption about why they were called) and page callbacks (that accept arguments from the URL). E.g. in my opinion, it's a mistake that the form builder function user_login issues a drupal_goto. – Oswald Sep 20 '12 at 22:46

Implement hook_user_view() to show the uid on the user/x page.

| improve this answer | |

Start by copying drupal theme user-profile.tpl.php into your theme, make sure to keep the name as user-profile.tpl.php.

open the file and edit it the way you want, I usually use this to get the uid

global $user
$uid = $user->uid;

then maybe you want to do something like:

print $user->uid;

Dont forget to clear your cache.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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