$node->field_file gives me a FileFieldItemList while the first entry seems to be a FileItem.

$node->field_file->getValue() gives me an integer (I guess the file id). How can I actually get the downloadable file uri from that?


First you need to load it with the fid :

  $file = \Drupal\file\Entity\File::load($fid);

And after :

  $imageSRC = file_create_url($file->getFileUri());
  • 3
    You can use File::url() to make this shorter: $imageSRC = $file->url();
    – keithm
    Aug 25 '16 at 19:14
  • 2
    File:url() is deprecated as of 8.7.x. See: drupal.org/node/3019830
    – Keven
    Feb 7 '20 at 19:25

It is also possible to do one-liner with magic method:


Or to render as string

Option 1: $node->field_file->entity->url();

Option 2: file_create_url($node->field_file->entity->getFileUri());

to get the file intity

$file = \Drupal\file\Entity\File::load($fid);

to get the file url

$url = $file->url();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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