8

I'm building my first site with drupal. And I made a custom user field: Full name. Now I want to get the value of this fild in my template to say “Hello, %username%”. How do I do that?

Clive
  • 36,918
  • 8
  • 87
  • 113
Konstantin Bodnia
  • 1,372
  • 3
  • 20
  • 43

4 Answers4

19

Clive's answer is correct except that you should use field_get_items to get the values for a field. It will handle the language for you. You should also sanitize the value.

function THEME_preprocess_page() {
  global $user;
  $user = user_load($user->uid); // Make sure the user object is fully loaded
  $full_names = field_get_items('user', $user, 'field_full_name');
  if ($full_names) {
    $vars['full_name'] = check_plain($full_names[0]['value']);
  }
}

If your site uses the Entity API module, you can also use a entity metadata wrapper like this

function THEME_preprocess_page() {
  global $user;
  $user = user_load($user->uid); // Make sure the user object is fully loaded
  $wrapper = entity_metadata_wrapper('user', $user);
  $vars['full_name'] = $wrapper->field_full_name->get(0)->value(array('sanitize' => TRUE));
}

See also Writing robust code that uses fields, in Drupal 7

Pierre Buyle
  • 4,883
  • 2
  • 32
  • 31
  • 1
    missing `$` sign in the `entity_metadata_wrapper()` call should be `$user` instead of `user` – FLY Aug 13 '13 at 09:36
  • Hi Pierre, following code is not get any data. $user = user_load('72'); $first_name = field_get_items('user', $user, 'field_first_name'); – Ananth May 09 '14 at 04:48
  • If you don't get any data it means there is no value for the field on that entity. – Pierre Buyle Oct 29 '15 at 13:22
11

Depending on your setup/field name, something like this in template.php (preprocess function for the template file):

function mytheme_preprocess_page() {
  global $user;
  $user = user_load($user->uid); // Make sure the user object is fully loaded

  $vars['full_name'] = $user->field_full_name[LANGUAGE_NONE][0]['value'];
}

Then something like this in page.tpl.php:

if (isset($full_name) && !empty($full_name)) :
  echo 'Hello ' . $full_name;
endif;

Note that LANGUAGE_NONE may need to be changed if you're running a multi-lingual site.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • You should use field_get_items. Direct access can break on multi-language website or if the field structure change. – gagarine Nov 26 '12 at 08:29
  • @gagarine This was a long, long time ago. Field structures can't change FYI, there's no mechanism for it. – Clive Nov 26 '12 at 08:59
  • Does this work only in templates, or also in a module? when I load my user with `$account = user_load($uid);`, I only get empty arrays instead of the custom fields: `$account->pwyw_balance` is just an array() without content. Do custom fields need to be loaded with extra code? Same thing happens when I load the user using `$wrapper = entity_metadata_wrapper('user', $account)->value()`. `$wrapper['pwyw_balance']`is an empty array. – nerdoc Oct 29 '15 at 12:20
1

I know this question was asked quite a while ago, but I wanted to post an alternative. It looks like you want to change the field in the $variables array that is $variables['name'] to what you have in your custom field that I've called field_real_name. If you are using a preprocess function, there is no need to pull in the global $user object. You have access to the $variables array, so you can get the user information with this - it will load the information associated with the uid (see template_preprocess_username):

function mythemename_preprocess_username(&$variables) {
    $account = user_load($variables['account']->uid);
    ...more code will go here in a moment
}

If you dpm($account) (or kpr($account) if you aren't using devel) you will see that you have access to all of the user information, without using the global $user object.

Then you can change the output of $variables['name'] to be your field_real_name as follows:

function mythemename_preprocess_username(&$variables) {

  // Load user information with user fields
  $account = user_load($variables['account']->uid);

  // See if user has real_name set, if so use that as the name instead
  $real_name = $account->field_real_name[LANGUAGE_NONE][0]['safe_value'];
  if (isset($real_name)) {
    $variables['name'] = $real_name;
  }
}
kbrinner
  • 121
  • 5
1

We can add the below code anywhere in the template file.

<?php
global $user;
$user = user_load($user->uid);
print $user->name;
?>