0

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.
$webform_submission->save();

}

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,

1

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