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
Comment #1
wOOge CreditAttribution: wOOge commentedComment #2
wOOge CreditAttribution: wOOge commentedFixed formatting.
Comment #3
wOOge CreditAttribution: wOOge commentedComment #4
Dev1.addweb CreditAttribution: Dev1.addweb at AddWeb Solution Pvt. Ltd. commentedFor 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.
Feel free to connect me incase of any concern regarding this.
Thanks!
Comment #5
wOOge CreditAttribution: wOOge commented@Deepali_Agarwal — thanks for the quick reply — however that code doesn't work either. Same result — no change to the user's Profile2 field. :(
Comment #6
jigish.addweb CreditAttribution: jigish.addweb at AddWeb Solution Pvt. Ltd. commentedIf you are using term reference/entity reference for your field 'field_member_status', please use below code for saving your profile.
Hope this helps you.
Thanks!
Comment #7
arnoldbird CreditAttribution: arnoldbird commentedThe solutions in #4 and #6 do not work.
Comment #8
arnoldbird CreditAttribution: arnoldbird commentedComment #9
RickJ CreditAttribution: RickJ commentedI'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.