3

If I am viewing a users profile page, how do I obtain and display (in a block) their uid? NOTHING to do with the current logged in user. I need to display the users id in a block on their page.

5 Answers 5

6

If you are showing a standard drupal user profile page this should get you going:

  $user = user_load(arg(1));
  $user->uid; // access user id

This assumes the path is user/uid. If your path looks different adjust the arg(position_in_path) to your needs.

1
  • 1
    I am unsure why BetaRite uses the user ID (already in the path) to load the user object only then to output the user ID. Use this instead. if (arg(0) == 'user' && is_numeric(arg(1))) $userid = arg(1);
    – 2pha
    Feb 7, 2014 at 3:00
3

A better option would be to use template_preprocess_user_profile(&$variables) and add the user id from here - that way you're not relying on any URL arguments (arg() is a recipe for fail).

Pop the following in your template.php

function MYTHEME_preprocess_user_profile(&$variables) {

  $account = $variables['elements']['#account'];
  //Add the user ID into the user profile as a variable
  $variables['user_id'] = $account->uid;
  // Helpful $user_profile variable for templates.
  foreach (element_children($variables['elements']) as $key) {
    $variables['user_profile'][$key] = $variables['elements'][$key];
  }

  // Preprocess fields.
  field_attach_preprocess('user', $account, $variables['elements'], $variables);

}

This will make the UID available to the user-profile.tpl.php template as a variable called $user_id.

This means that you can then copy user-profile.tpl.php from the modules/user directory to your theme directory and change it to the following:

<div class="profile"<?php print $attributes; ?>>
  <?php print $user_id; ?>
  <?php print render($user_profile); ?>
</div>

Obviously you'll want to style it and add extra DIVs and whatnot, but at it's most basic this will put the user id at the top of the user profile page.

4
  • I'm not sure it is the answer on @Jamie's question. Anyway, user-profile.tpl.php already has User Id available.
    – kalabro
    Jul 5, 2012 at 8:57
  • Aah...I missed the 'display in a block' part. I did a print_r on the $user_profile variable and didn't see the UID there. If it was part of the render array, then surely it would be rendered. Either way, the above code works fine and does allow for the UID to be rendered onto the page.
    – Chapabu
    Jul 5, 2012 at 9:15
  • Yup, just checked. By default, UID is not part of the $user_profile render array.
    – Chapabu
    Jul 5, 2012 at 9:21
  • nobody forbids you to print $variables['elements']['#account']->uid; in user-profile.tpl.php, but it's not the answer I think. Your code is useful, so I haven't downvoted it :)
    – kalabro
    Jul 5, 2012 at 9:29
1

You can use this function menu_get_object('user')

0

probably not the best approach in drupal 8 but this work

$current_path = \Drupal::service('path.current')->getPath();
    $exploded_path= explode('/',$current_path);
    $uid=end($exploded_path);
    if (prev($exploded_path) =='user')
      $user=User::load($uid);
-3

as BetaRide in the page profile you can use arg(1) it will give you user profile and then you can use user_load()

2
  • 1
    OP says: NOTHING to do with the current logged in user. But global $user does give you exactly this.
    – BetaRide
    Jul 5, 2012 at 8:26
  • 1
    yey I think the question need the logged in user sorry
    – Ahmed
    Jul 5, 2012 at 9:22

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.