When a batch of images are uploaded through the panel, they appear in alphabetical order in the files section. If the order is never changed through the panel, the images are never given a sort number.
If you then use images->sortBy('sort', 'asc') in a template, the images rendered on the page do not match how they appear in the panel (I’m not sure exactly what the rendered order is from). This can be fixed by re-ordering any of the images in the panel, but that seems like a bug or unnecessary step for the end user.
Hey @texnixe
Lets say a template is using a images->sortBy('sort', 'asc'). If a user uploads multiple files at once via the panel and does not manually sort them, the file order shown in the panel does not match the order rendered on the site. I would expect the order to be the same in both places.
Hm, the default sort order is by file name. That’s what you should get on the frontend if you don’t sort. I’m not quite sure what happens if you sort by a sort field that doesn’t exist.
@texnixe let me try to clarify, and let me know what you think. I just tested this out with the Starterkit by creating a new Album page and uploaded 7 images at once.
Here is the order the images are shown in the panel:
This is corrected as soon as you drag one image to a new position, but I’ve had clients get confused and not realize that they needed to do that. I’m not sure where the sort order on the front end is coming from, so it seems like a bug or an enhancement. Thanks
We have also troubles with this … my customer uploads images and then after upload grab the sort handle, move it 1px to left and then it’s working again. Seems definitly a bug …
I found a better solution for this. Looking at the Kirby source (kirby/config/api/routes/files.php), you can pass multiple parameters to sortBy. It will fallback to filename if sort order does not exist.
foreach($page->images()->sortBy('sort', 'asc', 'filename', 'asc') as $image):
This should really be the default behavior in Kirby, if manual sorting is turned on. Otherwise users upload something, think it’s in the correct order already (as it shows like that in the panel) and then it shows unexpectedly in a different order in the frontend. Seems like a bug to me.