6

In other words: How do I get the URI for each of the files in a file field that's on a custom content entity called 'listing'.

Right now I have:

$images = $variables['elements']['photos']['#items'];

Where $images is a FieldItemList

How do I iterate over my FieldItemList to grab the URI for each of the files referenced therein?

Ok figured first part out with:

 $images = $variables['elements']['photos']['#items'];
   $photos = $images->getIterator();
   foreach ($photos as $photo) {
     xdebug_break();
   }

Now the question is:

How to get the URI for each of $photo, and assign it as a variable...?


Update

After Berdir's Comment and my own investigation this is what I went with.

  if (isset($variables['elements']['photos']['0'])) {
    $number_of_photos = count($variables['elements']['photos']['#items']);
    $listing_pics = [];
    for ($i = 0; $i < $number_of_photos; $i++) {
      $pic = file_create_url($variables['elements']['photos'][$i]['#file']->getFileUri());
      $listing_pics[] = $pic;
    }
  $variables['elements']['listing_photos'] = $listing_pics;
  }

This is in a Hook_template_preprocess_HOOK(), and now all of the urls from my multi-value file field are available to the template like this {{ elements.listing_photos }}

1 Answer 1

13

PHP calls getIterator() for you, you don't have to do that. Also use $variables['node'], much easier to access.

File/Image references an extension of entity references, which means they inherit the computed ->entity property, which automatically loads the referenced entity. Make sure you check if the file can be loaded, or inconsistent data (if, for some reason, the file gets deleted. Less likely for files but could happen for node or other references) would result in a fatal error.

foreach ($variables['node']->photos as $item) {
  if ($item->entity) {
    $uri = $item->entity->getFileUri();
  }
}

$uri is something like 'public://your_file.jpg'. You can create a public url with file_create_url() or you can also pass it to an image style entity's buildUrl() method to get the URL for a given image style.

See http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet for common ways to access field values, what properties exist for each field type and much more.

3
  • Thanks Berdir, the link to the cheat sheet was helpful. I should have clearly stated that I was not using a node. I updated my question with what I ended up going with. May 14, 2016 at 16:38
  • Node or not, the structure definitely looks like you are working in an entity template. user, comment, custom entity... it all works the same. Just $variables['user'] or whatever it is in your case instead of node. Should still be easier than your example.
    – Berdir
    May 15, 2016 at 6:15
  • Correct link to cheat list: web.archive.org/web/20160222055646/http://wizzlern.nl/sites/…
    – Dimetry
    Jan 24, 2022 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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