Hi,

I have a template for the userprofiles. Each field is outputted by:
render($user_profile['field_myownfield'])

How can I output the user's email? I was told that I should use "$user->mail", but that gives me the email address from the user who is viewing the profile.

Thanks

Comments

Mr_Zoidberg’s picture

This did the job:

$usr = user_load(arg(1));
print $usr->mail;
agsparta’s picture

Hi,

Can you paste the whole user-profile.tpl.php page. I am trying to do the same on drupal 7 website.

Thank You,

Ashish

Mr_Zoidberg’s picture

Just use:

City:

print render($user_profile['field_city'])

Your field:

print render($user_profile['field_yourfield'])

etc.

Create your own html and insert the "print render($user_profile['field_yourfield'])" :)

lukasho’s picture

Hi,

sorry I'm a newbie (to theming and also to Drupal). How do I exactly do this? I added a file called user-profile.tpl.php in the templates folder of the theme I'm working with and put one such php statement in, as you suggest. But the user profile looks exactly as it did before. Do I need to activate the template somewhere?

Thanks a lot,
Lukas

lsdoc77’s picture

If you look at the comments at the beggining of the file it says that :

* Available variables:
* - $user_profile: An array of profile items. Use render() to print them.
* - Field variables: for each field instance attached to the user a
* corresponding variable is defined; e.g., $account->field_example has a
* variable $field_example defined. When needing to access a field's raw
* values, developers/themers are strongly encouraged to use these
* variables. Otherwise they will have to explicitly specify the desired
* field language, e.g. $account->field_example['en'], thus overriding any
* language negotiation rule that was previously applied.
*

So if you print_r $user_profile in you template file it will give you the variables for the user you are currently looking..
Or even better

<?php
  print '<pre>'. check_plain(print_r($user_profile)) .'</pre>';
?>
Sagar Ramgade’s picture

Hi,

Using user_load(arg(1)) is a bad idea, I used a preprocess function which we need to the active theme's template.php to achieve this :

<?php
/**
 * Process variables for user-profile.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $account
 *
 * @see user-profile.tpl.php
 */
function YOUR_THEMENAME_preprocess_user_profile(&$variables) {
  $account = $variables['elements']['#account'];
  // Helpful $user_profile variable for templates.
  foreach (element_children($variables['elements']) as $key) {
    $variables['user_profile'][$key] = $variables['elements'][$key];
  }
  //Add mail to $user_profile variable
  $variables['user_profile']['mail'] = $account->mail;
  // Preprocess fields.
  field_attach_preprocess('user', $account, $variables['elements'], $variables);
}
?>

Then in your user-profile.tpl.php, use $user_profile['mail'] to show email address.

Regards
Sagar
?>

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

NicoDruif’s picture

Works like a charm!

ressa’s picture

Just add a code field, and insert [user:mail] in the 'Field code' field. You can find other tokens below this field.

mttjn’s picture

Please could you clarify what you mean by "code field" in this context? Thanks

ressa’s picture

Sure, there is some more information here: Add custom fields to a display.

carlodimartino’s picture

Thanks for that. Works a treat.