Using a custom webform handler, I am trying to copy an image from the image field (field_usr_avatar_img) located at the user account page to the image element (avatar_img) located at the webform submission of the a webform where my custom handler is set.

In my custom handler, I have tried the below:

use Drupal\user\Entity\User;
use Drupal\file\Entity\File;

public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

$userid = 25;
$userprofile = \Drupal\user\Entity\User::load($userid);

// Get the image id.
$avatarimg = $userprofile->field_usr_avatar_img->id()

// Set webform submission Data.
$webform_submission->setElementData('avatar_img', $avatarimg);  

// Save the updated webform submission.


However, I ended up with below error:

Error: Call to undefined method Drupal\file\Plugin\Field\FieldType\FileFieldItemList::id() in Drupal\my_custom_module\Plugin\WebformHandler\customWebformHandler->submitForm() (line 61...

Line 61 is: $avatarimg = $userprofile->field_usr_avatar_img->id()

any help on how to copy the image from an entity to another ?

Thank you,

closed as unclear what you're asking by kiamlaluno May 26 at 11:22

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


You must call ->entity to get the file entity, then you can call ->id() like this, $userprofile->field_usr_avatar_img->entity->id().