9

I added a dozen or so new fields (in Drupal 7) to the user profiles using:

Configuration -> People -> Account Settings -> Manage Fields (admin/config/people/accounts/fields)

How can I access these values? They do not seem to be part of $user

Tried, without success:

global $user;
var_dump($user);

5 Answers 5

13

I think I found it. There is one additional step it seems.

global $user;
$user_full = user_load($user->uid); // user_load(uid) returns the complete array
var_dump($user_full);

I now have access to my custom profile fields through $user_full

1

Panels can also do this quite painlessly with relationships.

0

I'm reposting my answer from https://stackoverflow.com/questions/8124089/get-value-of-custom-user-field-in-drupal-7-template here since I think this is an alternative solution. This example shows how to use something like field_real_name instead of the default username.

If you are using a preprocess function, there is no need to pull in the global $user object. You can 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. 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;
  }
}
0

For some weird reason, the profile fields in Drupal 7 are not what they used to be. However, the user profile object makes the additional profile fields accessible as array elements. For example:

$profile->field_fieldname['und'][0]['value']

is not available but it will work when rewritten like this:

$user_profile['field_fieldname']['#object']->field_fieldname['und'][0]['value'];

So I simply did the following in my code:

/*
 * Create simplified variables as shortcuts for all fields.
 * Use these variables for read access lateron.
 */
$firstname = $user_profile['field_first_name']['#object']
  ->field_first_name['und'][0]['value'];

$middlename = $user_profile['field_middle_name']['#object']
  ->field_middle_name['und'][0]['value'];

$surname = $user_profile['field_surname']['#object']
  ->field_surname['und'][0]['value'];

$image = $user_profile['field_user_picture']['#object']
  ->field_user_picture['und'][0]['uri'];

Just another way of getting things working, instead of calling the $user object yet again.

0

You can load user data (custom fields included) with Drupal 7 core

$user = entity_load($entity_type = "user", $ids = Array($user->uid), $conditions = array(), $reset = FALSE);

More details at Drupal 7 > API > Entity load

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.