DennisA
December 10, 2021, 4:13pm
1
Hello,
I am having a grid where I simple can put in multiple thumbnail images and videos of one project. The problem is that, when I delete everything in the files field on the panel, then the video is still visible in the grid and running. When I then trying to add new images, they doesn’t appear and the video is still visible.
<?php foreach(page($children)->children()->listed() as $project) : ?>
<?php if($video = $project->files()->filterBy('type', 'video')->first()): ?>
<div>
<figure>
<a
href="#"
data-url="<?= $project->url() ?>"
data-project="<?= $project->url() ?>">
<video
src="<?= $video->url() ?>"
type="<?= $video->mime() ?>"
data-title="<?= $project->title() ?>">
</video>
</a>
</figure>
</div>
<?php elseif($image = $project->files()->filterBy('type', 'image')->first()): ?>
<?php foreach ($project->homepageimages()->toFiles() as $image) : ?>
<div>
<figure>
<a
href="#"
data-project="<?= $project->url() ?>" data-url="<?= $project->url() ?>">
<img
data-title="<?= $project->title() ?>"
src="<?= $image->resize($width, null, $quality)->url() ?>"/>
<div class="mobile-title"><?= $project->title() ?></div>
</a>
</figure>
</div>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>
This is the field:
homepageimages:
label: Homepage Image Thumbnails
type: files
layout: list
Maybe someone experience the same problem?
texnixe
December 10, 2021, 4:34pm
2
Are you using Kirby’s cache, maybe?
Also, the if-elseif statements can be shorter:
<?php if($video = $project->videos()->first()): ?>
<?php elseif($image = $project->images()->first()): ?>
Must say that I don’t quite get the logic of the elseif statement.
DennisA
December 10, 2021, 4:37pm
3
Thank you very much. I am not using the Kirby cache. Also the cache folder is empty.
I was thinking to use the if-elseif statement to check if its an image or a video.
texnixe
December 10, 2021, 4:52pm
4
Here you check the project has a video and output it if yes. Otherwise…
…you check if the page has an image, and if yes, you loop through the files selected in the homepageimages field.
So you want to output either one video, or the all images in the field but only if an image exists.
But I think that is somehow superfluous can could simply be:
<?php foreach(page($children)->children()->listed() as $project) : ?>
<?php if($video = $project->videos()->first()): ?>
<div>
<figure>
<a
href="#"
data-url="<?= $project->url() ?>"
data-project="<?= $project->url() ?>">
<video
src="<?= $video->url() ?>"
type="<?= $video->mime() ?>"
data-title="<?= $project->title() ?>">
</video>
</a>
</figure>
</div>
<?php else: ?>
<?php foreach ($project->homepageimages()->toFiles() as $image) : ?>
<div>
<figure>
<a
href="#"
data-project="<?= $project->url() ?>" data-url="<?= $project->url() ?>">
<img
data-title="<?= $project->title() ?>"
src="<?= $image->resize($width, null, $quality)->url() ?>"/>
<div class="mobile-title"><?= $project->title() ?></div>
</a>
</figure>
</div>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>
DennisA
December 10, 2021, 5:05pm
5
I tried this. But I think I described the problem wrong.
I was trying to show only the files in the “homepageimages” fields, where I can select images and videos which should be shown. I just tried to loop though it but it also isn’t working:
<?php foreach(page($children)->children()->listed() as $project) : ?>
<?php foreach ($project->homepageimages()->toFiles() as $video) : ?>
<div>
<figure>
<a
href="#"
data-url="<?= $project->url() ?>"
data-project="<?= $project->url() ?>">
<video
src="<?= $video->url() ?>"
type="<?= $video->mime() ?>"
data-title="<?= $project->title() ?>">
</video>
</a>
</figure>
</div>
<?php endforeach ?>
<?php foreach ($project->homepageimages()->toFiles() as $image) : ?>
<div>
<figure>
<a
href="#"
data-project="<?= $project->url() ?>" data-url="<?= $project->url() ?>">
<img
data-title="<?= $project->title() ?>"
src="<?= $image->resize($width, null, $quality)->url() ?>"/>
<div class="mobile-title"><?= $project->title() ?></div>
</a>
</figure>
</div>
<?php endforeach ?>
<?php endforeach ?>
texnixe
December 10, 2021, 5:12pm
6
Ah, that’s a different story:
<?php foreach(page($children)->children()->listed() as $project) : ?>
<?php foreach ($project->homepageimages()->toFiles() as $file) : ?>
<?php if ($file->type() === 'video'): ?>
<div>
<figure>
<a
href="#"
data-url="<?= $project->url() ?>"
data-project="<?= $project->url() ?>">
<video
src="<?= $file->url() ?>"
type="<?= $file->mime() ?>"
data-title="<?= $project->title() ?>">
</video>
</a>
</figure>
</div>
<?php elseif ($file->type() === 'image' ) : ?>
<div>
<figure>
<a
href="#"
data-project="<?= $project->url() ?>" data-url="<?= $project->url() ?>">
<img
data-title="<?= $project->title() ?>"
src="<?= $file->resize($width, null, $quality)->url() ?>"/>
<div class="mobile-title"><?= $project->title() ?></div>
</a>
</figure>
</div>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
1 Like
DennisA
December 10, 2021, 5:17pm
7
Thank you very much! This makes sense. I understand it now !!