So I’ve ran into an issue where I’m not able to pull the image from an custom upload field. Note: The destination is in a custom folder and the images are uploading fine on the backend.
Hey Hugoaf, thanks for the response, however, your example results in an error. One because there is an extra “]” added to this line but even removing the extra “]” I get this
an exception has been thrown during the rendering of a template (“Array to string conversion”)
This example code didn’t work either. Still produced an error.
Unexpected token “punctuation” of value “]”."
It has to be the syntax is wrong or its not possible to use media objects in this way. I can add the media object with the name of the jpg statically but I’m trying to do it in a dynamic way because this code will live in an include…so this isn’t an authorable region on the page.
I know if there are images upload via the default upload field you can pull the file with
page.media
and thats if the image is being uploaded to the pages>images folder. But the destination on my field is different. I’m surprised there isnt an example / recipe of how to do this on the Grav Doc.
@paul Yeah man I’ve been looking at this recipe and I don’t think it addresses what I am asking. Currently I can pull everything from the header except the fields with upload functions. And it may be because of how my for loop is and/or the destination of the images.
If I was to simply do:
{% for feature in page.find(‘/events’).children() %}
{{ feature.title }}
{% endfor %}
it outputs all the titles from each child page of the events folder, as expected. No issues.
But, it doesn’t seem to pull the image file, path, name or anything from the field that I use to upload images…
{% for feature in page.find(‘/events’).children() %}
{{ feature.header.overview.homepage_img.name }}
{% endfor %}
The above twig code doesn’t error out but it results to blank. And it should at least print out all the names of the image files right?
If I were to target just the field since its within the page header like so:
{% for feature in header.overview.homepage_img %}
{{ feature.name }}
{% endfor %}
This will pull the image name from the header…but that’s not what I need. I’m trying to go through all the child pages of the events folder and pull the images from the field above. The field above is simplified for this question, but I’m only allowing one image to be uploaded to this field. I’m sure there is something simple I’m missing or overlooking.
The answer is in the cookbook, but let me explain.
When using file fields, don’t forget that the file field allows for multiple images to be uploaded, it therefore create an array of image, even if you only allowed for a single image.
In this example
{% for feature in page.find(’/events’).children() %}
{{ feature.header.overview.homepage_img.name }}
{% endfor %}
you did not add the first filter, so Grav won’t know which image name it should output
{% for feature in page.find(’/events’).children() %}
{{ feature.header.overview.homepage_img|first.name }}
{% endfor %}
Notice the first filter added.
This will output the image name of the uploaded file.
Now if you haev the image name, you just have to add page.media to retrieve it.
On the page:
{% set this_year = "now"|date('Y-m-d') %}
{% for feature in page.find('/events').children.order('feature.header.overview.datestart', 'desc') if feature.header.overview.datestart > this_year %}
{% if loop.index < 4 %}
<div class="event">
<div class="event-img">
<a href="#" title="{{ feature.title }}">
<img src="{{ '/' ~ feature.header.overview.homepage_img|first.path }}" />
</a>
</div>
<div class="event-content">
<h5>{{ feature.title }}</h5>
<p>{{ feature.header.overview.datestart }}</p>
<p><a href="#" title="See more about {{ feature.title }}">View Details</a></p>
</div>
</div>
{% endif %}
{% endfor %}
Would have preferred a prettier way of looping that image but if it works it works. @paul if you have any more suggestions I’ll appreciate your input alot. Thanks for your previous replies.
The debugger shows 3 null entries. Even the order is still acting wonky from the previous post you helped me on. It seems to only order the folders and not the data itself. I wonder if it has to do with the loop itself for both issues.