In Drupal 6, I could use the following code to display user profile fields

$profile =  profile_load_profile($user); 
print $user->profile_firstname ; 

However, in Drupal 7, I can define additional user fields , (e.g. field_firstname) using Configuration, People, Account Settings, Manage fields. Now how do I get data from the user fields, say field_firstname as profile_load_profile() is not applicable in Drupal 7?

Thanks for your help.

Comments

tax14’s picture

I have managed to get data from the custom user fields with the following code. This code directly extracts data from the underlying tables.

 global $user;
$firstname = db_query("SELECT field_firstname_value  FROM {field_data_field_firstname} 
   where entity_id= :uid", array(':uid' =>$user->uid))->fetchField();

$lastname = db_query("SELECT field_lastname_value  FROM {field_data_field_lastname} 
   where entity_id= :uid", array(':uid' =>$user->uid))->fetchField();

  print  t("Welcome: " . $firstname. ' ' . $lastname) ; 

I feel that there is some built-in function to get the field values that I am not aware of. If anyone knows about that function, please let me know.

zd370’s picture

global $user;
$user_fields = user_load($user->uid);

$firstname = $user_fields->field_firstname['und']['0']['value'];
$lastname = $user_fields->field_lastname['und']['0']['value'];

print  t("Welcome: " . $firstname. ' ' . $lastname) ; 
tax14’s picture

Chintal Desai,

Thanks for providing the code. It works.
Thanks again for sharing.

zd370’s picture

NP... cheers!!!

Anonymous’s picture

Do you know, how to ask all the custom profile fields as an array?? Trying to figure that out for a long time now.

Zach’s picture

Do you mean to create an array for all user fields?

hockey2112’s picture

How can I add this kind of functionality to the uc_order-customer.tpl.php file for Ubercart? I have a user account field called "Allergies" that I need to print on the order invoice. I tried your code sample above, but it did not work.

Thanks!