I have a custom content type with a file field. I am trying to attach a file uploaded through a custom form to the custom content type. I am able to load the node and update all the other values of the node except the file field.

I found Realityloop: Programmatically attach files to a node in Drupal 8, but it doesn't seem to work.

This was what was suggested in the above link

$node = Node::create([
  'type' => 'article',
  'title' => 'Druplicon test',
  'field_image' => [
  'target_id' => $file->id(),

This is what I have tried inside submitForm

$fid = $form_state->getValue('file_field_name')[0];
$file = File::load($fid);
$node = Node::load($nid);     
$node->set('file_field_in_node', $file->id);

That was setting the file as permanent in the files table but the node did not have the file.

I also tried to save with

$node->set('file_field_in_node', $file);

That did not help either. I am also curious as to which is the file/function that handles the set function for the file field.


[SOLVED] Not sure what was wrong earlier. Both the approaches seems to be working correctly now. So setting the file_id or the file object into the file field for the node seem to be saving the node correctly with the file attached to it.

  • Are you usign core's Media module to manage your files? – No Sssweat Nov 6 '19 at 8:19
  • No. I added a file field to a content entity. I am using a custom form to collect data for the entity and am creating and saving the entity on form submit. I am not using any other modules / APIs so far. – anoopjohn Nov 6 '19 at 8:23
  • 2
    Since this question seems to be about code, we need to see the code you wrote. Without that, we cannot tell you what is wrong in the code. – apaderno Nov 6 '19 at 10:50
  • Thanks for the pointers. I have updated the code. I was also able to get this working. I will update the question with the solution as well. – anoopjohn Nov 7 '19 at 0:54
  • Not sure how to restructure the question to document the answer / question correctly so that this will help somebody. Inputs appreciated. – anoopjohn Nov 7 '19 at 1:02

If you use media core module to manage files, you have already uploaded your file, and you want to attached it to filed eg field_file

    $node = Node::load(NID);
    $node->set('field_file' , ['target_id' => FID]);

Note: FID is your file id.

  • Thanks for the reply @berramou. I am not using the media module. I am only looking to attach files to a content type and keep it in the system. Not sure if I need to use the media module. – anoopjohn Nov 7 '19 at 0:34
  • BTW this automatically takes care of file_usage or do we have to do that explicitly? – anoopjohn Nov 7 '19 at 4:48

In your custom form use code like this:

$form['imagefile'] = [
      '#type' => 'managed_file',
      '#title' => $this->t('Imagefile'),
      '#default_value' => $this->configuration['imagefile'],
      '#weight' => '0',
      '#upload_location' => 'public://images/redaktion/',

And here's a piece of code how it works low level:

// check if the folder exists
$folder = date('Y-m', time());
$data = file_get_contents($imagefilepath);
if (!is_dir("/home/rainer/src/asdent/web/sites/default/files/images/pics/{$folder}")) {
// create the file entity
$photo = file_save_data($data, "public://images/pics/{$folder}/".$filename, FILE_EXISTS_REPLACE);

// later in your entity::create:
'myimage' => ['target_id' => $photo->id()],

The trick is to create the file entity beforehand and then link the custom entity to it.

  • Sorry, this is very basic. If you're using D8 standard form API with a '#type' => 'managed_file' there should not be any problem. – Rainer Feike Nov 6 '19 at 9:57
  • Thanks for input Rainer Feike. I was able to get this working. – anoopjohn Nov 7 '19 at 1:01

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