Custom pages method with sortBy

Hello,

I am writing a custom pages sorting method:

<?php
Kirby::plugin('jaume/pagesMethods', [
    'pagesMethods' => [
        'sortBySurname' => function () {
            return $this->sortBy(function ($child) {
                // xplode
                $words = explode(' ', $child->title()); 
                // take maximum two words
                $words = array_slice($words, 0, 2);
                // obtain the last one 
                $last_word = array_pop($words);
                // use it to sort
                return $last_word;
            }, 'asc');
        }
    ]
]);

This seems to work on the fronted, using it like this, as it returns the sorted collection.

$page->children()->sortBySurname()

But I also want to use it on a pages section in the panel, and it does not seem to work there, I tried:

sections:
  artists:
    type: pages
    sortBy: page.children.sortBySurname

and also

sections:
  artists:
    type: pages
    sortBy: sortBySurname

But it does not seem to sort as in the frontend, so I assume it is not working at all, but it does not throw any exception.

Thank you

Ah, no, of course I am mixing pageS with pagE methods.

The provided custom pages method, works with pages.

But on a pages field, I have to use a custom pagE metod, singular, so I can do:

<?php
Kirby::plugin('jaume/pageMethods', [
    'pageMethods' => [
        'sortBySurname' => function () {
            $all = $this->parent()->children()->sortBySurname();
            return $all->indexOf($this);
        }
    ]
]);

which works…

Btw, Is there any quicker way to obtain all siblings including $this than going through the parent ?

Thank you

It’s called siblings()

$this->siblings() includes $this ?

I thought not :sweat_smile:

1 Like