Sortable files section does not return sorted collection

I have an implicitly sortable files section in my blueprint

imagenes:
  headline: Imágenes
  type: files
  layout: cards
  template: imagen

The ‘imagen’ template:

title: Imagen
accept: image/*
fields:
  alt:
    label: Texto alternativo
    type: text
    width: 1/1
  caption:
    label: Pie de foto
    type: text
    width: 1/1

I access this files section in my template as such:

<?php foreach ( $page->images() as $img ): ?>
  etc...
<?php endforeach ?>

But the order of images in the panel and in the html output does not match, see:

panel:


page:
image

I would bet the order they show in is the order in which they were uploaded but did not fully test this

Is this the expected behaviour?

I cannot see where may I be affecting the sorting of these images, nor any mention in the reference about this being a default behaviour.

Some light please

Thank you

By default the images are sorted by filename. To have them come out on the front end template in the sort order set by the field, you need to sort them that way on the front end.

$images = $page->images()->sortBy('sort', 'asc');
1 Like

I see, thank you @jimbobrjames.

Is this perhaps specified somewhere in the reference? Because I could not find it, for example in $page->images() or $page->files() or in their respective source codes.

Thank you

It’s in the sorting cookbook guide…

1 Like

But there it does not say that default is filename sorting, does it ?

Actually it would lead me to believe that to achieve filename sorting I have to do:

// sort by filename
$files = $page->files()->sortBy('filename');

Thank you

Well no, it doesnt specifically mention the default. However, Kirby generally defaults to alpha sorting on titles or filenames whenever you work with a group of something, unless you tell it otherwise.

Good to know. I’ve opened an issue anyway for consideration https://github.com/getkirby/getkirby.com/issues/846

Thank you!

There’s an info text here: https://getkirby.com/docs/guide/content/files#rendering-files-in-your-templates

1 Like