I have an array of images that I’d like to output the URL’s of. I’m using;
<?php foreach($data->gridImages() as $items): ?>
<div class="grid__image">
<?php dump($items) ?>
</div>
<?php endforeach; ?>
Which outputs this:
Array
(
[0] => erasmus.png
[1] => may-ninth.png
)
How do I turn these into actual files? I’ve tried a lot of variations for the past hour or so and can’t crack it.
I’ve also tried;
<?php foreach($data->gridImages() as $items): ?>
<div class="grid__image">
<?php foreach($items as $item): ?>
<?php dump($item) ?>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
But if I try something like toFile()
I get an error warning me about strings.
Yeah, it’s via this part of the blueprint;
pageImages:
type: files
headline: Available Images
layout: list
info: "{{ file.dimensions }}"
template: image
Because I’m using the ‘builder’ plugin, I’m using this section to upload a bunch of images that can then be placed where you like using the following blueprint;
label: Grid
fields:
gridImages:
type: files
label: Included Photographs
layout: cards
captions:
label: Captions
type: toggle
text: Show captions next to each image?
width: 1/2
color:
label: Grid Background Colour (optional)
type: color
editableAlpha: false
default: "#efefef"
presets:
- "#efefef"
width: 1/2
Using this plugin: https://github.com/TimOetting/kirby-builder
You can fetch images from a files field using the toFiles()
method as explained in the docs I linked to:
<?php foreach($data->gridImages()->toFiles() as $file): ?>
<div class="grid__image">
<?php echo $file->url() ?>
</div>
<?php endforeach; ?>
Echoing the url
here without an image doesn’t make much sense, so you would have to adapt your HTML or just echo $file
.
2 Likes
The toFiles()
function was a big one. I was using toFile()
in the same way but getting an error, didn’t realise I just needed to pluralise
For a files field that is limited to having only one file, toFile()
makes more sense, because toFiles()
gives you a collection, whereas toFile()
returns a single file object if it exists.
1 Like