Set a Document File in a webform submission programmatically I have writen a PHP class that saves a pdf file programmatically. The class contains a function that returns the Drupal path private://documentA.pdf

I would like to attach the file to a webform submission:

$webform_submission = \Drupal\webform\Entity\WebformSubmission::load(ID);
$data = $webform_submission-&gt;getData();
$data['file_attachment'] = //retrieve the file
$webform_submission-&gt;setData($data);
$webform_submission-&gt;save();


Webform element type of file_attachment is Document file.
To attach the file to a webform submission, you should get the file id first, and then set the file_attachment to file id.
Try this out.

  $webform_submission = \Drupal\webform\Entity\WebformSubmission::load(1);
  $data = $webform_submission-&gt;getData();
  // Get the document file object from uri.
  $document = \Drupal::entityTypeManager()
    -&gt;getStorage('file')
    -&gt;loadByProperties(['uri' =&gt; 'private://documentA.pdf']);
  // Get the file Id.
  $doc_id = reset($document)-&gt;id();
  // Set the file_attachment to fid. 
  $data['file_attachment'] = $doc_id;
  $webform_submission-&gt;setData($data);
  $webform_submission-&gt;save();