Order of results returned by query

I’m using a select field to allow the user to select links internally to the site.

My blueprint looks like:

link:
    label: Link
    type: select
    options: query
    query:
	fetch: pages
	value: '{{parent}}/{{uid}}'
     	text: '{{parent}}/{{title}}'

I’ve noticed that if my site hierarchy is something like:

  • azores
    • uniform
    • xray
  • zebra
    • alpha
    • bravo

you would expect the query to return:

/azores
/azores/uniform
/azores/xray
/zebra
/zebra/alpha
/zebra/bravo

But what you actually get is:

/zebra/alpha
/azores
/zebra/bravo
/azores/uniform
/azores/xray
/zebra

because they sort order is by the uid of the page without it’s path, so /zebra/alpha is before /azores

This makes a total mess of the site hierarchy and makes it very difficult to locate a specific page. Is there a way to get the query to return items sorted by their full path?

Unfortunately, the sort order (by title) is hardcoded in the Panel source code. So I’m afraid you’d have to modify it there (/panel/app/src/panel/form/fieldoptions.php).

public function items($page, $method) {

    if(!$page) return new Collection();

    switch($method) {
      case 'visibleChildren':
        $items = $page->children()->visible();
        break;
      case 'invisibleChildren':
        $items = $page->children()->invisible();
        break;
      case 'siblings':
        $items = $page->siblings()->not($page);
        break;
      case 'visibleSiblings':
        $items = $page->siblings()->not($page)->visible();
        break;
      case 'invisibleSiblings':
        $items = $page->siblings()->not($page)->invisible();
        break;
      case 'pages':
        $items = site()->index();
        $items = $items->sortBy('title', 'asc');
        break;
      case 'index':
        $items = $page->index();
        $items = $items->sortBy('title', 'asc');
        break;
      case 'children':
      case 'grandchildren':
      case 'files':
      case 'images':
      case 'documents':
      case 'videos':
      case 'audio':
      case 'code':
      case 'archives':
        $items = $page->{$method}();
        break;
      default:
        $items = new Collection();
    }

    return $items;

  }
```