Home » Php » php – Get value of custom user field in Drupal 7 template?

php – Get value of custom user field in Drupal 7 template?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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

Answer:

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;
  }
}

Answer:

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

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