By Romka on
Hello.
I added a several profile fields to user accounts in my Drupal 7 site, and I want to use the values from this fields in my code. I've written this code in my template.php:
function my_theme_preprocess_node(&$variables) {
$account = user_load($variables['user']->uid, TRUE);
dpm($account);
//$field = field_get_items('user', $tmp, 'profile_gp');
//dpm($field);
}
Now, when I viewing my node like a user with uid = 1, I can see values of all profile fields (screenshot: http://romka.eu/files/fields-scr-1.jpg), but if I viewing my node like an other user, with full permissions, I can't see fields values (screenshot: http://romka.eu/files/fields-scr-2.jpg).
I've tried to put this code in other places, for example in my module on hook_user_load, but I've got the same result.
What I made wrong?
Comments
Are you sure, the user(uid
Are you sure, the user(uid !=1) associated with nodes has filled these profile fields in the background?
Thanks, smiletrl, you're
Thanks, smiletrl, you're right. I thought that $variables['user'] in theme_preptocess_node function contains a object of node author, but it contains current user. I have to use $variables['uid'] instead of $variables['user']->uid.
profile2_load_by_user
A helpful function:
profile2_load_by_user
It will return an object containing the user's profile data, or an array of objects depending on how the function is called and whether there are multiple profiles in play.