6

We are using Drupal 7 and the Profile2 module.

We create and add a value to a field in the profile using this code:

$profile = profile_create(array('type' => 'main', 'uid' => $account->uid));
$lang = LANGUAGE_NONE;
$profile->field_headline[$lang][0]['value'] = 'Software Engg';
profile2_save($profile);

How can we programtically update the user profile field 'field_headline'?

Please advice us.

6

You just need to load the user using drupal function

profile2_load_by_user($account->uid, $type_name = NULL)

$account->uid => user id

$type_name => one of the profile types of profile 2 if any otherwise NULL

   $profile =  profile2_load_by_user($account, $type_name = NULL)
   $profile->field_headline[$lang][0]['value'] = 'update Software Engg';
   profile2_save($profile);

For more information Click Here

Hope it will work for you!

3
  • 1
    It is not working when i use for term refrence.
    – Fawwad
    Jan 16 '15 at 13:32
  • 1
    How would you change the profile2 type to something else? Apr 27 '15 at 15:21
  • This solution will not work in Drupal hooks such as hook_user_update().
    – arnoldbird
    Apr 6 '17 at 18:29
0

Update specific value of user

$user_fields = user_load($user->uid);    
$user_fields->field_points['und'][0]['value'] = $points;

user_save($user_fields);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy