I am getting quite desperate here. I can’t for the life of me figure out how to add an item to the end of a collection. I thought that is what append() is for, but I can’t get it to work. I tried add() and extend() as well, they don’t work either.
What I need to do is to (additionally) add a collection’s first and second item to the collections end again.
In the linked question the issue is mainly that the user wants to merge collections of different types. Surely there must be an easier way to add an item of the same type to the end of a collection?
Btw., the prepend() method works as I expected:
$myFiles = page('landingpage')->files()->template('media')->sortBy('sort');
$myFiles->prepend($myFiles->last()); // adds the last file to the beginning of the collection
so I don’t understand why the append method seems to fail altogether?
creates a files object that extends the general collection class and should inherit all the collection class methods. That’s why prepend() works for example. So why does append() not work?
That does not solve the problem. If I do
$slides = new Collection(page('landingpage')->files()->template('media')->sortBy('sort'));
$slides->append($slides->first());
I am working with files here, not with pages, but when I use the new Files() method instead and follow the advice from the other thread, it also does not work:
$files1 = new Files(page('landingpage')->files()->template('media')->sortBy('sort'));
$files2 = new Files(page('landingpage')->files()->template('media')->sortBy('sort')->limit(2));
$merged = $files1->add($files2); // still only has the length and content of $files1
Thinking in what you are tryin to do, maybe it’s because it is already on the collection so you can not add it again? maybe there is another approach to what you want to do? Why do you need to add the same files again?
Well, if that were the case, I think this should be considered a bug. I mean, why would a command like like append() not allow to add items that are already in the collection? Especially when prepend() does it just fine.
Because I need them to appear at the end of a slideshow again to enable a seemless rollover back to the start of the slideshow. I currently “solving” this by using two separate loops, but that can’t be the right way to do it, so I would still like to know what’s would be the designated way of doing this in Kirby (and also why append() and add() don’t work as expected).
But you might as well do your loop and then render the first file again, instead of trying to create a collection. I don’t see why you would need two loops.
Well, I have successfully used this prepend method to duplicate the last file for a few hours now. So is the bug rather in the prepend() function then?
I need to add the first two elements of the collection to the end, so I was using the second loop to again loop over the files collection with a limit(2) option.
So the easiest way to create this collection I came up with now was this:
This still seems incredibly cumbersome, just to add a few items to a collection/array. I guess I will just stick to my 2-loop-solution for now, all in all that seems cleaner to me.
Interesting, I tested similar code without success. Which Kirby version are you using? The file is put at the beginning but not duplicated inside the collection.
Excactly. That’s why I would go with either the array solution I suggested above or by simply looping through the files collection and then render the first item again.
Yes. That just makes the code really messy in my specific case, as I need to add the last item in the beginning as well as the first two items in the end, but I guess I will have to do it this way.
It would be nice, if Kirby allowed to add duplicates to a collection, but I understand that this is probably a limitation of php arrays and probably also not a requirement that pops up all too often.
One last thing I would like to mention though is that it would be nice, if the prepend() and append() method would produce some sort of error instead of just silently fail in such a case.
Don’t know if you will find this less messy, but you could create an array of files-objects and loop through this array and within the first loop through the objects: