1

I'm begining with Drupal 8 and I want to get the url of the photos in the content I create to display them.
The context is : a photograph upload pictures for a customer. This customer can see it on a page. To get this data in common I created a content for my shooting with an id.
What i did is :

$photos = $shooting->field_shooting_photos->target_id;

This allow me to get the id of every pictures uploaded. Now I want to do a foreach on $photos in my .module that will return every url in an array.
Then I will use this array in my Twig and do a foreach on it too to return every photos.
I just can't figure out how to do, I think I will have to use something like that

$path = $photo_id->getFileUri();

but I can't figure out the syntax to get it into an array.
Thank you for your help!

Solution :

Here is what I did in case it can help someone :

$photosTest = $shooting->field_shooting_photos->referencedEntities();

$photoUrls = [];

foreach($photosTest as $media) {
  $photoUrls[] =
  $photoUrls[] = [
      'render' => [
         '#theme' => 'image_style',
         '#style_name' => 'medium',
         '#uri' => $media->field_media_image->entity->getFileUri()
       ],
       'href' => $media->field_media_image->entity->getFileUri(),
     ];
}

In my Twig file I render the array :

<div>
 {% for photo in photos %}
    <a href="{{ file_url(photo.href) }}">{{ photo.render }}</a>
 {% endfor %}
</div>
1

Giving field_shooting_photos is a multi valued field of Files, this is how to generate your array of URI :

$photoUrls = [];
foreach($shooting->field_shooting_photos->referencedEntities() as $image) {
    $photoUrls[] = $image->getFileUri();
}

For media image :

$photoUrls = [];
foreach($shooting->field_shooting_photos->referencedEntities() as $media) {
    $photoUrls[] = $media->field_media_image->entity->getFileUri();
}

Note this will give you the File_id of the first image in the entite file reference list :

$shooting->field_shooting_photos->target_id;

NB : Best way is probably to generate render array or use built-in drupal themes, is none fit your needs, your may also generate a custom field formatter.

  • thank you, i'm trying your solution but I get a Call to undefined method Drupal\media\Entity\Media::getFileUri(), I probably need to do a load first no ? – StefanYYC Sep 24 at 7:58
  • That's why i said "Giving field_shooting_photos is a multi valued field of Files" you get an iterable of Media, so you have to deal with medias instead of File. I update my answer in few time. – Oulalahakabu Sep 24 at 8:11
  • Okay thank you, I'm searching on still – StefanYYC Sep 24 at 8:20
  • I gave you the piece of caode for media references in this answer. – Oulalahakabu Sep 24 at 8:28
  • 1
    Are you trying to render this array directly in your twig file ? That's not the way to render medias and/or images in drupal (have a look at my NB). Have a look at drupal.org/docs/8/api/render-api/render-arrays and drupal.stackexchange.com/questions/251844/…. – Oulalahakabu Sep 24 at 8:42

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.