Hi, I’ve set up StarterKit (Kirby 3.7.0.2) and added a Gallery page to it with Virtual Pages
I’ve altered the Photography > Album pages to select/upload images from the Gallery Page and have managed to get the selected child images from the Gallery displayed on the album pages. (Previous post:
Selecting images from another page)
I am now trying to pull through the virtual page URLs for the selected child images to the photography > album pages.
So far I have:
site/controllers/album.php:
<?php
return function ($page) {
$gallery = $page->content()->get('images')->toFiles()->sortBy('sort', 'filename');
return [
'gallery' => $gallery,
];
};
site/models/album.php
<?php
class AlbumPage extends Page
{
public function cover()
{
return $this->content()->get('cover')->toFile() ?? $this->image();
}
public function children()
{
$galleryImages = kirby()->site()->find('gallery')->images()->template('gallery-image');
$images = [];
foreach ($galleryImages as $image) {
$images[] = [
'slug' => $image->name(),
'num' => $image->sort()->value(),
'template' => 'gallery-image',
'model' => 'gallery-image',
'content' => $image->content()->toArray(),
];
}
return Pages::factory($images, $this);
}
}
site/templates/album.php
<?php snippet('header') ?>
<article>
<?php snippet('intro') ?>
<div class="grid">
<div class="column" style="--columns: 4">
<div class="text">
<?= $page->text() ?>
</div>
</div>
<div class="column" style="--columns: 8">
<ul class="album-gallery">
<?php foreach ($gallery as $image): ?>
<li>
<a href="<?= $image->url() ?>" data-lightbox>
<figure class="img" style="--w:<?= $image->width() ?>;--h:<?= $image->height() ?>">
<?= $image->resize(800) ?>
</figure>
</a>
</li>
<?php endforeach ?>
</ul>
</div>
</article>
<?php snippet('footer') ?>
I know I need to update the album template to retrieve the URL, but I’m struggling to get the correct data into the page model first.
This is a print of the $page data on an album page (with two gallery images selected):
AlbumPage Object
(
[children] => Kirby\Cms\Pages Object
(
[0] => photography/trees/blood-moon
[1] => photography/trees/cheesy-autumn
[2] => photography/trees/coconut-milkyway
[3] => photography/trees/dark-forest
[4] => photography/trees/desert-tree
[5] => photography/trees/island-from-above
[6] => photography/trees/last-tree-standing
[7] => photography/trees/monster-trees-in-the-fog
[8] => photography/trees/nasty-rocks
[9] => photography/trees/sharewood-forest
[10] => photography/trees/stay-in-the-car
[11] => photography/trees/tent-in-the-woods
)
[content] => Kirby\Cms\Content Object
(
//Other items removed to save space
[images] => - gallery/desert-tree.jpg
- gallery/last-tree-standing.jpg
)
)
I feel like this is almost there, but I’m not sure how to filter the children to only use the urls of the selected images (in this case desert-tree.jpg and last-tree-standing.jpg) AND correct the path. For example, changing from “photography/trees/IMAGE” to “gallery/IMAGE”.
If I print $galleryImages = kirby()->site()->find('gallery')->children()->template('gallery-image')
I can then see the list or urls I’d like to filter.
Kirby\Cms\Pages Object
(
[0] => gallery/blood-moon
[1] => gallery/cheesy-autumn
[2] => gallery/coconut-milkyway
[3] => gallery/dark-forest
[4] => gallery/desert-tree
[5] => gallery/island-from-above
[6] => gallery/last-tree-standing
[7] => gallery/monster-trees-in-the-fog
[8] => gallery/nasty-rocks
[9] => gallery/sharewood-forest
[10] => gallery/stay-in-the-car
[11] => gallery/tent-in-the-woods
)
However, if I change $galleryImages in model/album.php to use children() instead of images() like so: $galleryImages = kirby()->site()->find('gallery')->children()->template('gallery-image')
I then only get:
AlbumPage Object
(
[children] => Kirby\Cms\Pages Object
(
[0] => photography/trees/
)
)
Ideally I’d like to have the $page data on the an album page look like this:
AlbumPage Object
(
[children] => Kirby\Cms\Pages Object
(
[0] => gallery/desert-tree
[1] => gallery/last-tree-standing
)
[content] => Kirby\Cms\Content Object
(
//Other items removed to save space
[images] => - gallery/desert-tree.jpg
- gallery/last-tree-standing.jpg
)
)
If I can get there, I’m sure I can then update template/album.php to correctly link to the virtual page.
Am I going about this the correct way? Any help would be much appreciated!