Get value of custom user field in Drupal 7 template?

20,099

Solution 1

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.

Solution 2

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

Share:
20,099
Konstantin Bodnia
Author by

Konstantin Bodnia

Updated on January 06, 2020

Comments

  • Konstantin Bodnia
    Konstantin Bodnia over 4 years

    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?

  • gagarine
    gagarine over 11 years
    You should use field_get_items. Direct access can break on multi-language website or if the field structure change.
  • Clive
    Clive over 11 years
    @gagarine This was a long, long time ago. Field structures can't change FYI, there's no mechanism for it.
  • FLY
    FLY over 10 years
    missing $ sign in the entity_metadata_wrapper() call should be $user instead of user
  • Ananth
    Ananth almost 10 years
    Hi Pierre, following code is not get any data. $user = user_load('72'); $first_name = field_get_items('user', $user, 'field_first_name');
  • nerdoc
    nerdoc over 8 years
    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.
  • Nadeem Ahmed
    Nadeem Ahmed over 8 years
    If you don't get any data it means there is no value for the field on that entity.