SortBy pages title in structure field?

I’m trying to sort a structure field alphabetically based on the title of a pages field within a structure, but setting the sortBy field property doesn’t seem to have the intended effect. Any idea what I’m doing wrong?

  network:
    label: Network
    type: structure
    sortBy: boardmember desc
    fields:
      boardmember:
        label: Members or Collaborators
        type: pages
        empty: select collaborators
        icon: user
        query: site.index.filterBy('template', 'in', ['member', 'collaborator'])  
        multiple: false
      role:
        label: Role
        type: text

Not tested:

$network        = $page->network()->toStructure();
$networkByTitle = $network->sortBy(function($item) {
  if ($member = $item->boardmember()->toPage() ) {
    return $member->title();
  }
}, desc);

Texnixe, does your code go in a page model or controller? Or in a template? Would it then also be sorted alphabetically in the panel?

Oh, sorry, I thought you wanted to sort on the frontend. Reading closely can be of advantage.

If you want to sort the entries in the panel, you could create a method with that code in a model and then try and use that method as the field to sort by. Not sure if that will work.

See the docs on page models for details: https://getkirby.com/docs/guide/templates/page-models

Sorry, I should have been more verbose in my original question :slight_smile:

I have something like this going now, but it doesn’t seem to be doing anything (nor is it throwing any errors, so I’m wondering if the page model function is triggered by the panel)

site/blueprints/pages/article.yml

... (other blueprint stuff)
fields:
      network:
        label: Network
        type: structure
        sortBy: sortCollaborators
        fields:
          boardmember:
            label: Members or Collaborators
            type: pages
            empty: select collaborators
            icon: user
            query: site.index.filterBy('template', 'in', ['member', 'collaborator'])  
            multiple: false
          role:
            label: Role
            type: text 

and in site/models/article.php:

<?php

class ArticlePage extends Page {
    public function sortCollaborators() {
        $network = $page->network()->toStructure();
    
        return $network->sortBy(function($item) {
            if ($member = $item->boardmember()->toPage()) {
                return $member->title();
            }
        }, desc);
    }
}

Any thoughts?

Sorry. Thinking again, it doesn’t make sense at all, because we are dealing with a structure field, not a page.

On a side note, $page must be $this in a model, but you can throw that model away now.

Maybe use a custom collection method instead? New since 3.4: https://getkirby.com/docs/reference/plugins/extensions/collection-methods

I’ll give that a try. Funnily enough, sortBy: role (using the role field) works like a charm… So it’s the pages field type that seems to throw Kirby off…

Yes, because it is stored in yaml format.