drupal 8 programmatically load or add user profile

Public

profile module

Get raw version
php
  1. $profile_type = 'wechat_user_data';
  2.  
  3. $profile = \Drupal::entityManager()->getStorage('profile')
  4. ->loadByUser($user, $profile_type);
  5. if(!$profile) {
  6. $profile = \Drupal\profile\Entity\Profile::create([
  7. 'uid' => $user->id(),
  8. 'type' => $profile_type
  9. ]);
  10. }
  11. foreach($user_info as $field_key =>$value){
  12. $profile->set('field_wxuser_'.$field_key, $value);
  13. }
  14. $profile->save();