I am trying to do a form override in a custom module and need to extract information from a Profile2 field - the field is a user reference and I need to get the UID of the referenced user.

If the profile is called 'editor' and the Profile2 field is called 'author_reference', what do I need to do to get the UID?

I tried

global $user;
$profile = profile2_load_by_user($user->uid);
if ($profile['editor']->field_author_reference) {
		$author_uid = $profile['editor']->field_author_reference['und'][0]['value']; 
	} 

Comments

aitala created an issue. See original summary.

rickj’s picture

The content of a reference field is not in 'value', it's in 'target_id'.

aitala’s picture

So this?

$author_uid = $profile['editor']->field_author_reference['und'][0]['target_id'];

rickj’s picture

Yep, that should work.

rickj’s picture

Status: Active » Closed (outdated)