By rzegwaard on
Hi, i've created some extra user-fields (Home » Administration » Configuration » People » Account settings). Now i want to store data in these fields programmatically. But whatever i try, i don't get it working...
This is what i've done:
global $user;
// load user
$existingUser = user_load($user->uid);
// edit field-data
$existingUser->field_somefield['und'][0]['value'] = 'blah';
// save existing user
$user = user_save((object) array('uid'=>$user->uid), (array) $existingUser);
Has anybody a hint what i do wrong? or a good example for drupal 7?
Comments
Maybe this helps
This is what works for me:
Is this good practice?
I want to do the exact same thing, but for some reason it seems to me like we are doing it wrong if we have to add ['und'][0]['value'] to a field name in order to modify it. What does that even mean? Isn't Field API supposed to make things simpler?
I guess my main question is: Is this the right way to do this?
_
the [und] has to do with language translation ('und' = undefined iirc), the [0] is the index of the individual item in the field, and the [value] is which of the properties of field you wish to use.
Thanks for the info. I
Thanks for the info. I thought more about my problem and distilled my question down to this:
I looked at the Field API expecting to find something like field_set_value() for setting the value of a field.
Continuing the above example, I was hoping there would be something like:
field_set_value($edit['field_somefield'], 'blah');rather than:
$edit['field_somefield']['und'][0]['value'] = 'blah';Is there a function like my first example or is my second example the correct way to do things?
_
ah... interesting idea, but I don't know. Easy enough to find out though: http://api.drupal.org/api/search/7/field_
I totally agree - it seems
I totally agree - it seems like a "hacky" solution to use $edit['field_somefield']['und'][0]['value']
Replace 'und' with "language"
Every entity has a language declaration, so you can instead say:
LANGUAGE_NONE
Hey, use LANGUAGE_NONE instead of 'und'.
Breaks password
I tried the above ( user 1 - Core 7.21 ) - and it stopped accepting my password
Using this Drupal 6 suggestion...
http://drupal.org/node/976888#comment-3731202
...fixed it.
e.g.
$existing = user_load($uid);
unset($existing->pass);
$edit = (array) $existing;
$edit['field_somefield']['und'][0]['value'] = 'blah';
user_save($existing, $edit);
I take this back
The code above was logging me out erratically. I should have been passing the $user obtained using global $user to user_save() - not the user obtained from user_load().
The code above is now working as...
global $user;
$existing = user_load($uid);
unset($existing->pass);
$edit = (array) $existing;
$edit['field_somefield']['und'][0]['value'] = 'blah';
user_save($user, $edit);
OH MY GOD THANK YOU
This worked for me in 2017 still. I tried a whole bunch of other ways and this was the only way I could update a user field programmatically.
Use Entity Wrapper
Once you have the user object use the metadata_wrapper to work with fields.
If you want to read the field data
But you must install Entity API
But you must install Entity API module (http://drupal.org/project/entity) to use this... just writing as reminder for beginners
Assuming anyone's still
Assuming anyone's still tracking this thread:
I'm kinda late to this party, but: is the entity approach better in terms of performance than the user_save thing described above? It strikes me that loading and saving the whole freakin' user object has to be pretty costly, but I don't know enough about the comparable internals to judge.
Used this technique and it
Used this technique and it worked perfectly! thanks!
Need similar functionality
I'm trying to update some 'custom' user profile fields when an account is created.
I'm using shibboleth, and can get a hold on the vars I need.
I created a field named field_data_field_profile_union_code in the new drupal7 way of adding a field to user.
I have a module working.
In my module I want to call a hook after the new user is created via shibboleth. (I THINK It's hook_user_insert)
then I want to call something like the above code, where I can set my new drupal field to the shibboleth value e.g.
field_data_field_profile_union_code=>($_SERVER['User_union_code'] ), then save it and have Drupal update teh default account of the just created user with the shibboleth values for the custom fields.
I keep running into the white-screen-of-death however. I can see the shibboleth variables by calling:
...drupal_set_message("Hello, ".($_SERVER['PHP_AUTH_USER'] )." ...
I can also call global $user;
BUT as soon as I start calling user_load, I get WSOD!
Is the above code not meant for a module?
Thanks
There is a typo in the code
The incorrect part of the code:
// save changes
$obj-save();
The corrected version:
// save changes
$obj->save();