# Better handling of collections of files and images

**URL:** <https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 12, 2015, 4:50pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482 "2015-11-12T16:50:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![diondiondion](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/diondiondion/32/16754_2.png) [@diondiondion](https://forum.getkirby.com/u/diondiondion)\
**Post date:** [November 12, 2015, 4:50pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/1 "2015-11-12T16:50:10Z")

</div>

Hi!

There’s one thing about Kirby that always gets me because I expect it to be super easy to do with Kirby’s API, but when I get around to building it, it turns out to be not quite as simple:

Working with collections of files and images, especially across several pages and when they have to be merged.

Today, it took me quite a lot of googling and searching in this forum to figure out how to get a collection of all files in a page, including those of its children (& their children, & theirs…)

It’s extremely easy to get one level of files from a page: `$page->files()`, and if you want to output files in the order they’re nested, it’s no problem to loop over children (or index) of a page to do this.

But if you’re after a big collection of files that you can filter, search, limit or shuffle independently, there’s not much in Kirby’s API that helps you with that.

Eventually, the following two forum topics helped me finish my task, but the current solution breaks some beloved functions like Kirby’s `$files->shuffle()` and is not documented outside the forum.

> [@Get all images from children](https://forum.getkirby.com/t/get-all-images-from-children/1746):
>
> 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 argumen…

> [@Appending to Files collections](https://forum.getkirby.com/t/appending-to-files-collections/1803):
>
> I had two-subpages with Files collections. I needed to take the first 6 files from A and then append the shuffled remainder of A and B files. This could have been accomplished with more loops in the template, but I wanted to compose a single Files collection and have a single loop in the template. - Page - Subpage A - Subpage B I discovered that the Pages collection as a nice add() method that as per [this post](http://forum.getkirby.com/t/merge-collection-of-pages/766), but the Files collection doesn’t have any facilities to append to it. I ca…

So, to wrap this up: It would be great if dealing with multi-level file/image collections was possible in a more Kirby-ish way. 😄

Some suggestions:

- There could be something like `$pages->files()`, which would return all files from the pages collection. Then it would be easy to get all nested files or images of any page like this: `$page->index()->files()`.
- Easier merging & adding of collections, for example like so: `$images->add($moreImages)`

Another note:  
The cheatsheet entries for `$site->images()`, `$site->files()` etc. are so short that they can easily be mis-understood:

> Returns a $files collection with all files in /content

It’s quite easy to wrongly assume this to mean that it returns **all** files within /content, even those nested in subpages. Obviously it’s not what it does, so a more specific description would be welcome. 😁

Thanks!

PS: Actually, this should probably extend to pages collections as well. For example, `$pages->add()` is not in the cheatsheet, and the stuff discussed in [Showing all posts from different folders (merging collections) - #8 by texnixe](https://forum.getkirby.com/t/showing-all-posts-from-different-folders-merging-collections/1679/8) could also be kirbyfied somehow (`merge($pages, $morePages)`) & would deserve a mention in the cheatsheet.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 12, 2015, 6:45pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/2 "2015-11-12T18:45:37Z")

</div>

> [@diondiondion](#):
>
> $images-\>add($moreImages)

Random wild guess how this could be realized at the moment:

```auto
$images = new Collection(a::merge($images->data, $moreImages->data));

```

---

<div class="post-metadata">

**Author:** ![diondiondion](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/diondiondion/32/16754_2.png) [@diondiondion](https://forum.getkirby.com/u/diondiondion)\
**Post date:** [November 13, 2015, 11:17am UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/3 "2015-11-13T11:17:19Z")

</div>

> [@distantnative](#):
>
> Random wild guess how this could be realized at the moment:

Thanks, that saves me a foreach loop compared to the [previous solution](https://forum.getkirby.com/t/get-all-images-from-children/1746/6). 😄

I’d still like to campaign for a solution that’s more in line with the rest of Kirby’s API and can be added to the docs, as I’d imagine it to be a quite common need, and I’m always surprised not to find anything about these things in the cheatsheet.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2015, 11:39am UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/4 "2015-11-13T11:39:00Z")

</div>

I agree, if we had a way to easily combine collections while preserving sorting, shuffling etc., that would definitely make things a lot easier. 👍

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 13, 2015, 12:04pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/5 "2015-11-13T12:04:51Z")

</div>

What would be a good name for this in the collection class?

```auto
public function combine($collection2) {
    $collection = clone $this;
    $collection->data = a::merge($collection->data, $collection2->data);
    return $collection;
  }

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [November 13, 2015, 12:06pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/6 "2015-11-13T12:06:09Z")

</div>

I think we could use `merge` as the method name to keep it consistent with other Kirby (Toolkit) methods.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 13, 2015, 12:06pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/7 "2015-11-13T12:06:48Z")

</div>

I’m in for `merge` as well.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 13, 2015, 12:07pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/8 "2015-11-13T12:07:54Z")

</div>

Yea, I was a little hesitant, cause `merge` is used rather as a static method in the toolkit so far.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 13, 2015, 1:00pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/9 "2015-11-13T13:00:12Z")

</div>

Opened up two PR as ideas for `$files->merge($moreFiles)` and `$pages->files()` – not sure if they are really useful though.

> <https://github.com/getkirby/toolkit/pull/112>

> <https://github.com/getkirby/kirby/pull/317>

---

<div class="post-metadata">

**Author:** ![diondiondion](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/diondiondion/32/16754_2.png) [@diondiondion](https://forum.getkirby.com/u/diondiondion)\
**Post date:** [November 13, 2015, 1:00pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/10 "2015-11-13T13:00:13Z")

</div>

Cool 😄

Merge sounds good. Will that also work for page collections, or only for files?

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [November 13, 2015, 1:01pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/11 "2015-11-13T13:01:01Z")

</div>

That would work also for pages collections as it’s a change on the collection class from the toolkit.

---

<div class="post-metadata">

**Author:** ![diondiondion](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/diondiondion/32/16754_2.png) [@diondiondion](https://forum.getkirby.com/u/diondiondion)\
**Post date:** [November 13, 2015, 1:06pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/12 "2015-11-13T13:06:01Z")

</div>

Awesome, thanks for these! 😄

They’ll definitely come in handy.

---

<div class="post-metadata">

**Author:** ![diondiondion](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/diondiondion/32/16754_2.png) [@diondiondion](https://forum.getkirby.com/u/diondiondion)\
**Post date:** [November 13, 2015, 5:42pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/13 "2015-11-13T17:42:55Z")

</div>

> [@distantnative](#):
>
> Opened up two PR

…aaand they’ve already been merged / implemented. This is seriously great, I’m looking forward to 2.2!

Thanks again 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:04pm UTC](https://forum.getkirby.com/t/better-handling-of-collections-of-files-and-images/2482/14 "2025-02-06T17:04:13Z")

</div>


