I'm importing users from another DB. I can create the user object and save it just fine. I'm just wondering how to populate the profile fields now.
I'm importing users from another DB. I can create the user object and save it just fine. I'm just wondering how to populate the profile fields now.
Comments
Comment #1
mmilano commentedFigured it out:
Comment #2
joachim commentedAssuming we have documentation pages, it'd be really useful if you had time to add that to a page, or start a new one on working with profile2 programatically :)
Comment #3
ubdr commentedwhat does $profile->field_firstname['und'][0]['value'] denotes ?
Comment #4
dan3h commented'und' means "undefined language". See Gábor's post about it
Note that for clarity, you can/should use drupal's constant, LANGUAGE_NONE instead of the string 'und'. So...
$profile->field_firstname[LANGUAGE_NONE][0]['value']Comment #5
dan3h commentedIf you doing this to import users, you can use the User Import Framework (which is where I learned about 'und'). It's a nice balance of pre-written module, and hooks for you as a programmer to import your data how you want.
Comment #6
fagoyou can also use the entity API wrappers, e.g. this is what Rules uses.
Comment #7
southweb commented// Create a profile type; add a field; populate it.
Comment #8
bxCreative commentedGreat stuff - thanks for this.
PS - for version 7.x-1.1, function
profile2_by_uid_loadshould beprofile2_load_by_user.Comment #9
pwaterz commentedI dont think this needs to be in the documentation. I dont even think node module has documentation on how to programmatically create nodes. Also it's pretty straight forward if you have worked with creating any entity programmatically.
Comment #10
zulfierken commentedSorry to open again.
I am trying to update profile2 field programmatically. Using:
$main_profile->field_name[LANGUAGE_NONE][0]['value'] = $form_state['values']['name'];
$saved_profile = profile2_save($main_profile);
works great and change the value.
At the other hand this does not change the safe_value. Should I add one more line to change safe value also (if this is the case why there is safe value :) )
For now, adding
$main_profile->field_name[LANGUAGE_NONE][0]['safe_value'] = $form_state['values']['name']; is enough or not? Are there any other places that I should change.
I think profile update function should update related fields including safe values automatically.
Comment #11
pwaterz commentedI believe you need to add the safe value as well. This has nothing to do with the profile module. It has to do with fields api.
Comment #12
derherrberger commentedThx mmilano, your post helped me a lot!
A little update: in Profile2 Version 7.x-1.2 the function profile_create() is marked as "Deprecated. Use profile2_create()." (profile2.module Line 278)
Comment #13
ludo.rIn my opinion, a better way to populate profile fields would be :
So that you don't care about setting fields specificities like :
NB : I have tested with textfields, but not with link or email fields, it may require an array as a value to set.
Comment #14
swim commentedJust a side note to profile2 field population. When updating field data from a profile object make sure you reference the desired profile type when saving.
Fairly obvious ofc but hopefully it helps.
Comment #15
daveX99 commented[edited to fix name of function in #1, below... -davex99]
[edited a 2nd time to remove extra closing parenthesis in code block... -davex99]
The code above (#7) was very helpful, but I have 3 quick comments with regards to how to create/load profile object:
I think this is because of the call to load the profile AFTER creating it, but not yet having saved it -- it loads an empty object.
So, instead, my version of that code looks like this:
Also, FWIW, I did not have to set the 'safe_value' of the field array - setting the 'value' was enough, but I was setting an integer value.
-dave.
Comment #16
joachim commentedComment #17
thirstysix commentedThanks.