With the new Drupal 7 API, merging profile fields needs to be done at the field level. In my Drupal 7 port, I tried to do this but haven't been able to successfully re-save the user object using fields. Here's the in-progress code. Any help would be much appreciated.

/**
 * Implement hook_usermerge_merge_users() using the new field API
 * TODO: Debug why not working. Fields appear to be correctly retrieved
 * but values are not saving to the kept_user object.
 */
function userfield_usermerge_merge_users($user_to_delete, $user_to_keep) {
    
  //WORKING: Grab all fields in the user bundle.
  $fields = field_read_instances(array('bundle'=>'user'),  array());

  //WORKING: For each field, retrieve the field name.
  foreach ($fields as $field) {
  $current_field = $fields[0]['field_name'];   //returning the field name

   //START OF NOT WORKING: Check if if the current field has a value for the $user_to_keep.
   if (isset($kept_user->{$current_field}['und']['0']['value'])){
        //If there's a value, do nothing.
            
   } else { //If there's not a value, check for a value for user_to_delete.
           $deleted_user = user_load($user_to_delete->uid); //Load user_to_delete.
           
         //Check if the deleted user has a value for that field.    
        if (isset($deleted_user->{$current_field}['und']['0']['value'])){

         $kept_user = user_load($user_to_keep->uid); //Load the kept user.
         $edit = (array) $kept_user; //Create an edit array for the kept user.
         
         $edit[$current_field][$kept_user->language][0]['value'] = $deleted_user->{$current_field}[$deleted_user->language]['0']['value'];  //Save the field from the deleted user to the kept one.
                  
         $kept_user = user_save($kept_user, $edit); //Re-saving the kept user.

        }
        else {//If the deleted user does not have a value for the field, do nothing.
        }
   }
  
  }
}

Comments

antiorario’s picture

Version: 6.x-1.x-dev » 7.x-2.0
Status: Active » Closed (fixed)

This issue should be fixed with 7.x-2.0.