I want to print a field from the profile of the current user programatically. I've got global $user....now how to I get to profile 2 field?

Comments

bjalford’s picture

tried this:

global $user;
$test = profile2_load_by_user($user->uid);
dpm($test);

but get undefined function

ron williams’s picture

I replaced
dpm($test);
with
print_r($test);

Do you have devel installed?

ron williams’s picture

Status: Active » Postponed (maintainer needs more info)
Saoirse1916’s picture

I've done this method as well and I can successfully print_r($test), in this case. I can see the full array of profile variables, but when I print them it breaks my site. Its as if it somehow strips out all CSS and crashes the rendering at the point that I try to print the variable that I need. Here's my code:

$account = user_load($node->uid);
$profile = profile2_load_by_user($account);
print $profile['main']['field_first_name']  

?>

The print causes the crash. If I do this:

print drupal_render(field_view_field('profile2', $profile['main'], 'field_first_name'));

It renders just fine, but it includes the label "First Name:" which I don't want.

Saoirse1916’s picture

Status: Postponed (maintainer needs more info) » Active
pwaterz’s picture

Status: Active » Closed (fixed)

$profile is an object not an array. Try using dsm or print_r on $profile. There is not bug here, just lack of understanding or php.

Saoirse1916’s picture

You're right -- I figured it out. Here's the code that did it for me.

 $account = user_load($node->uid);
$profile = profile2_load_by_user($account, 'main');
print '<h4>By: ' . $profile->field_first_name['und']['0']['value'] . '&nbsp;' . $profile->field_last_name['und']['0']['value'] . '</h4>';
jay.lee.bio’s picture

Issue summary: View changes

Saoirse1916, thanks for #7. If anyone knows what the code should be for Location, please let me know: https://www.drupal.org/node/1702180, #3