0

I am trying to update the user profile picture using entity_metadata_wrapper. But I am getting

"Unknown data property picture." error.

When I check the user property info, I cant see the picture field as well. Below is my example code:

$user = entity_metadata_wrapper('user', $form_state['entity_id']);
if (isset($form_state['values']['user_profile_pic'])) {
   $file = file_load($form_state['values']['user_profile_pic']);
   $file->status = FILE_STATUS_PERMANENT;
   file_save($file);
$user->picture = $file;
}

user entity wrapper

Thanks in advance.

0

Try this out :


  $wrapper = entity_metadata_wrapper('user', $form_state['entity_id']);
//- Get the user object.
  $user = $wrapper->value();
  if (isset($form_state['values']['user_profile_pic'])) {
    $file = file_load($form_state['values']['user_profile_pic']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    $edit = array(
      'picture' => $file,
    );
    user_save($user, $edit);
  }

|improve this answer|||||
  • Thanks berramou, but I am still getting "Unknown data property picture." entityMetadataWrapperexception error while debugging with xdebug. When I use $user->getPropertyInfo(), i cant get the picture field property associated with user, may be I think that is where something is wrong. I checked the /admin/config/people/accounts page and enabled the user pictures as well. But could not figure out what is the root cause. – nirmal lama Sep 26 '18 at 14:15
  • Try this i edit my answer. – berramou Sep 26 '18 at 14:16
  • Thanks berramou, really appreciate it. I did fix the issue with similar approach but I am not sure if it is the correct way. You can see that I have already declared $user variable as entity_metadata_wrapper in my example and declaring another user object with user_load function to save the picture is not the best way?? Again not sure about this, just wanted to know your thought. Thanks a lot. – nirmal lama Sep 26 '18 at 14:46
  • Okay, if you want to use entity_metadata_wrapper instead of user_load, see the code bellow, i have edited my answer again :) Note wrapper->value() to get the user object, it's important. Good Luck. – berramou Sep 26 '18 at 15:03
  • You are Welcome. – berramou Sep 26 '18 at 15:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.