Challenge:
When the user saves their account, I want check what role they have, then based on that set a value for a Profile2 list field (or text field).

Problem:
Setting the Profile2 list field (or any field... I tried a simple text field as well) is completely ignored on hook_user_update. I even tried triggering the update with Rules (React on: after user updated, Action: call php function). Just to test, I placed the exact same list field on the core User Profile, and used a similar script to update the field on user save/update and it worked... So something is wrong with Profile2's implementation of the field when saved.

Expectation:
To be able to set the value of a field on user save/update for Profile2 fields either via hook_user_update or via Rules.

Code:
Here is the code I used to try and update the Profile2 version of the list field (Not working):

// 	PROFILE2
	$account = user_load($account->uid);
	$profile = profile2_load_by_user($account->uid, 'profile'); 
	$wrapper = entity_metadata_wrapper('profile2', $profile->pid);
	if (in_array('Student', $account->roles)) {  
		$termTID = 3942; // TID of the term reference list item we want selected for the user
	}
	$wrapper->field_member_status->set($termTID);
	$wrapper->save();

Here is the code I used to update the Core Profile version of the same field (Working):

// 	CORE PROFILE
	$account = user_load($account->uid);
	$wrapper = entity_metadata_wrapper('user', $account);
	if (in_array('Student', $account->roles)) {  
		$termTID = 3942; // TID of the term reference list item we want selected for the user
	}
	$wrapper->field_member_status->set($termTID);
	$wrapper->save();

Am I missing something — or does Profile2 have issues saving data at time of user save/update? — or is what I'm trying to do just not possible with Profile2?
Any ideas to get me pointed in the right direction would help!

Comments

wOOge’s picture

Issue summary: View changes
wOOge’s picture

Issue summary: View changes

Fixed formatting.

wOOge’s picture

Issue summary: View changes
Dev1.addweb’s picture

For this, you need to use hooks for Profile2 module, and not user_save.

Please use following code snippet for updating your user profile with profile2.

$account = user_load($uid);
$profile = profile2_load_by_user($account, NULL);

// In thi $profile, update your fields you want.
if (in_array('Student', $account->roles)) {
        $termTID = 3942; // TID of the term reference list item we want selected for the user
        $profile->field_member_status[LANGUAGE_NONE][0]['value'] = $termTID;
}

// save user profile
profile2_save($profile);

Feel free to connect me incase of any concern regarding this.

Thanks!

wOOge’s picture

@Deepali_Agarwal — thanks for the quick reply — however that code doesn't work either. Same result — no change to the user's Profile2 field. :(

jigish.addweb’s picture

If you are using term reference/entity reference for your field 'field_member_status', please use below code for saving your profile.

$account = user_load($uid);
$profile = profile2_load_by_user($account, MACHINE_NAME_OF_PROFILE);

// In thi $profile, update your fields you want.
if (in_array('Student', $account->roles)) {

	$termTID = 3942; // TID of the term reference list item we want selected for the user
	$profile->field_member_status[LANGUAGE_NONE][0]['tid'] = $termTID;

	// save user profile
	profile2_save($profile);
}

Hope this helps you.

Thanks!

arnoldbird’s picture

Title: Profile2 fields do not accept changes on hook_user_save() » Profile2 fields do not accept changes on hook_user_update()

The solutions in #4 and #6 do not work.

arnoldbird’s picture

Priority: Normal » Major
RickJ’s picture

Status: Active » Closed (cannot reproduce)

I've tested the OP's example code on release 7.x-1.5, and it works as expected. I don't know if the original problem was actually related to profile2, but it's not a problem now.