Store a field on a node I altered the node form and added an manage_file-field; then, I added a callback on the #entity_builders property.

$fid = $form_state-&gt;getValue('file')[0];
$file = File::load($fid);

$file-&gt;setPermanent();
$file-&gt;save();

// $node-&gt;set('field_file', $file);
$node-&gt;field_file-&gt;setValue([
  'target_id' =&gt; $file-&gt;id(),
]);


However this gives me the following error:


  The file used in the File field may not be referenced.


At this point, I cannot use file_usage, since the node is not yet saved. 

How can I solve this?
Set the status of the created file to something other than PERMANENT.

So replace

$file-&gt;setPermanent();


with

$file-&gt;status = 0;


The file field will then set the status to permanent and add the file_usage metadata.