Files sortBy order after initial upload

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.

The sort order only makes sense if you reorder files. What behaviour would you expect? After all, you can upload more than one image/file at once…

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:

00_dino-poop-mountain.jpg
1-hobbits-and-stuff.jpg
1.jpg
01_clouds-eat-mountain.jpg
02.jpg
20.jpg
that-green-looks-fake.jpg

And then here is the order they are shown on the site:

01_clouds-eat-mountain.jpg
that-green-looks-fake.jpg
00_dino-poop-mountain.jpg
02.jpg
20.jpg
1.jpg
1-hobbits-and-stuff.jpg

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

What if you don’t sort them on the front end? Is the order then the same as in the Panel?

If you don’t sort them in the front end template, then yes the order is the same in the front end and in the panel.

But the issue is that I want to have sorting enabled.

Yes, I can reproduce this but I don’t know why this happens. Maybe it’s a bug after all.

Maybe you could work around this by checking it the files have a sort field with a number assigned and depending on that either sort or not sort.

1 Like

I’ve come across the issue of sorting right now;

as far as I can tell, panel field renders the files like this: [...unsorted by filename, ...sorted by sort]:

Calling just $page->images() returns sort by name:

CleanShot 2020-07-23 at 21.27.46

And $page->images()->sortBy('sort') returns [...by date desc, ...by sort]

CleanShot 2020-07-23 at 21.26.14

In my case, I wanted manually sorted images, so I force sort in the metadata in an upload hook.

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 …

Here’s how I solved it as suggested by @texnixe:

$sortBy = $page->images()->first()->sort()->isNotEmpty() ? 'sort' : 'filename';

foreach($page->images()->sortBy($sortBy, 'asc') as $image):
...
1 Like

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):
4 Likes

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.

1 Like