-1

Would like to ask how I can output or display the image entities from my entity browser/entity view to the twig template. I'm using the media entity module on drupal and then using views to select and display the images that are selected.

Apologies if my explanation is bit sloppy since I'm still new to drupal.

Setup Media Bundle

enter image description here

Setup Entity Browser using View

enter image description here

Set View as a Widget for entity browser

enter image description here

Select Images

enter image description here

In this screenshot I've selected two images to be displayed. I'm using the code the below to try and display the entities but it seems incorrect

{% for image in node.field_slider_image  %}

            <img src="{{ file_url(image.entity.uri.value) }}">

{% endfor %}
2

For an image in a media entity you need one step more. Something like this should work:

{% for media in node.field_slider_image  %}

  <img src="{{ file_url(media.entity.field_image.entity.uri.value) }}">

{% endfor %}

But without debugging tools this is hard to find out. You have three nested entities and their field values, the node with the media field, containing media entities with the image field, containing file entities, where you finally find the uri field.

An easier options might be to let Drupal do the rendering by configuring this in UI and theme the field, not the node.

  • Sorry for my ignorance but I can't seem to follow your advice. Regarding about letting Drupal do the rendering – Noel Dec 4 '17 at 7:45
  • I mean the normal way to build websites with Drupal. Configure how the fields are displayed in UI, look for a module that may help you to format the field or theme the field in templates. Then you don't need to code loops like this, because Drupal does this for you. – 4k4 Dec 4 '17 at 8:01
  • I see, well right now I'm using the media entity module to bundle the images /assets for management. Will try to look for another before trying your suggestion. Really need to look for a way output the media entity to the twig. Already tried your solution but it only shows the base path upon inspecting – Noel Dec 4 '17 at 8:33
  • You know you have to find the correct machine names of the fields? field_image in the code is only an example. – 4k4 Dec 4 '17 at 8:40

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.