Allow manual sorting but show latest uploaded file at top

I have set up my blueprint so that manual sorting is possible (eg. by not adding a specific sortBy definition), and I have my template setup so that it sorts by that manually defined sort order.

However, when I upload a new image it gets shown at the bottom, because the sort number is added as the highest new sort number. This is to be expected. However for my client this behaviour is a little tedious because they have to scroll down and then move the image all the way to somewhere (probably more at the top).

Is there a way to make the last uploaded image appear at the top, e.g. by automatically giving it the sort number 1 (and moving the sort num down one for the rest of the already sorted files)?

You could use a hook (file.upload:after) to assign a sorting number after upload, which will automatically resort all other files.

1 Like

Thanks Sonja, never worked with hooks before, but I’ll give it a try …

So, I managed to get something working … strangely file.upload.after didn’t work, but this did …

'hooks' => [
    'file.create:after' => function ($file) {
         $file->update([
              'sort' => '0'
         ]);
    }
],

The simplest thing is to assign 0 as the sorting order for the new image … that way I don’t have to bother resorting the other images. Do you see any problem in doing that this way?

Sorry, my mistake.

You could also simply use $file->changeSort(1).

Kirby sorts starting with 1 instead of 0, so I’d stick with that.

1 Like

Thanks Sonja, yes that looks even simpler. How does Kirby handle two files which both have sort = 1 in the txt file … does it then go by filename or what does it use?

Duplicate numbers are sorted by filename then. This should usually not happen, though. When you sort manually the next time, the duplicate numbers should be resorted as well.

1 Like