Hi,
is there a way to get a $files
object containing all images of the children of a page?
I tried this but without success:
$galleryPage = $pages->find('galerie');
$galleryChilds = $galleryPage->children()->visible();
$galleryImages = new Collection();
foreach ($galleryChilds as $c) {
$galleryImages->add($c->images());
}
In this case $galleryImages
seems to be an empty collection. If I use append
instead of add
I get an error saying that theres a missing argument, but I don’t know how to handle that …
Best,
Tobi
PS: There’s no docs for add
and append
, is it?
I think the problem here is that your are trying to add several images at once. You need a second foreach loop within the first that fetches each individual image from a child, and then append that image to the collection.
I tried this already but unfortunately this results in an empty collection, too … this is/was my code:
$galleryImages = new Collection();
foreach ($galleryChilds as $c) {
foreach ($c->images() as $i) {
$galleryImages->add($i);
}
}
This should work:
<?php
$galleryImages = new ArrayObject();
foreach ($galleryChilds as $c) {
foreach ($c->images() as $i) {
$galleryImages->append($i);
}
}
It does, but then I can’t process the images further like this:
$galleryImages = $galleryImages->filterBy('filename', '!*=', '-big')->shuffle()->limit(6);
Can I make a Collection out of an ArrayObject?
PS: Sorry for not mentioning that earlier O:-)
I think I have found a solution thanks to the source code:
<?php
$galleryImages = new Collection();
foreach ($galleryChilds as $c) {
foreach ($c->images() as $i) {
$galleryImages->data[] = $i;
}
}
?>
Hm … kind of … at least we’re (better: you’re) getting closer. Everything (I need) works except I can’t use shuffle()
, i.e.
$galleryImages = $galleryImages->filterBy('filename', '!*=', '-big')->limit(6);
is fine but
$galleryImages = $galleryImages->filterBy('filename', '!*=', '-big')->shuffle()->limit(6);
isn’t: The resulting collection is empty …
(Btw. I had to replace $files
with $galleryImages
in your code)
Update: i found out that the collection is not empty. It just look like it because my browser didn’t show the error message except in the source code … here it is:
Fatal error: Call to a member function caption() on integer in […]/kirby (final)/site/templates/home.php on line 50
(caption
is a field of the image meta file)
Edit:
Yeah … I found a workaround at least … here’s my code
$galleryPage = $pages->find('galerie');
$galleryChilds = $galleryPage->children()->visible();
$galleryImages = new Collection();
foreach ($galleryChilds as $c) {
foreach ($c->images() as $i) {
$galleryImages->data[] = $i;
}
}
$galleryImages = $galleryImages->filterBy('filename', '!*=', '-big');
shuffle($galleryImages->data);
$galleryImages = $galleryImages->limit(6);
But maybe @bastianallgeier could take a look at it …