Jump to content

Sorting the subfolders before pages in the page tree


Recommended Posts

How could I define that the subfolders of a page are shown first and the children of the page then in the admin page tree? Now the children are shown first and if there are many pages inside a folder, the subfolders are shown on the last page. 

Link to comment
Share on other sites

The way I've done things like this is to create a text field, use a hook to add a sort code to that field, then set each relevant parent template to sort its children using that field.

The sort code would be whatever you'd normally sort by, but with a prefix that depends on whether the page has children or not.

So, I'd use something like the following (not tested) in ready.php, here assuming you'd basically sort on title:

$this->addHookAfter('Pages::saveReady', function($event) {
    $page = $event->arguments[0];
    if(in_array($page->template, array('template1', 'template2', 'template3'))) {
        if($page->numChildren() > 0) {
            $sortcode = "A-" . $page->title;
        ) else {
            $sortcode = "B-" . $page->title;
        $page->set('sortcode', $sortcode);

Of course, you could use a hook in a module instead.

And you can use the same basic technique to get just about any kind of sort order you want.

  • Like 3
Link to comment
Share on other sites

I haven't tried, but you may be able to do it by hooking on ProcessPageList::find, as discussed here:

Note that the pages would have to be loaded and the sort codes calculated each time the page tree was displayed, so I think the approach I have suggested would be more efficient – something that is discussed in the thread too.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...