10

$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?

11

First you need to load it with the fid :

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

And after :

  $imageSRC = file_create_url($file->getFileUri());
  • 2
    You can use File::url() to make this shorter: $imageSRC = $file->url(); – keithm Aug 25 '16 at 19:14
1

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

$node->field_file->entity->getFileUri();

Or to render as string

file_create_url($node->field_file->entity->getFileUri());

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.