Jump to content

Sorting Admin PageList Tree


drjones
 Share

Recommended Posts

Hi

I don't know if it's me.

I have searched the forum and have found some related issues but no valuable answers.

I just want to sort the page tree in admin in a natural order, like natsort does.

I have experimented with RecursiveIteratorIterator and array_walk_recursive and a usort with strnatcmp.

Nothing works as expected.

Do you guys have a solution?

I just want to sort my pages by page name in a natural way.

There has to be a switch for that!

Thanks for your help.

jones

Link to comment
Share on other sites

do you mean the first level pages under Home, or pages under a branch. I think it would be bad to sort by page name under home because you'd end up with stuff like 404 and admin at the top, would be annoying.

if you mean pages under a branch, you can do the sort settings for children. If that is not working there is probably a way you can hook and then sort them your own way

Link to comment
Share on other sites

Sorting is a build-in function of template. You may edit it in admin by editing any parent template like "home".

2 hours ago, drjones said:

I just want to sort my pages by page name in a natural way.

There has to be a switch for that!

edit parent template>Family tab>Sort settings for children>choose "name" under "Children are sorted by">check for reverse or not

Link to comment
Share on other sites

9 hours ago, Macrura said:

do you mean the first level pages under Home, or pages under a branch.

I mean under a branch.

9 hours ago, Macrura said:

I think it would be bad to sort by page name under home because you'd end up with stuff like 404 and admin at the top, would be annoying.

That's a good point. You're absolutely right.

 

I am building a webapp to manage code sticks.

Every stick has a color, a number and a programmed code.

Now have a branch called "Green" with a template named "color". Under that page I have pages that have a template called "stick" with a number as page title (and name) and a field called code.

The sticks should be sorted by number in a natural way site wide (that means in page reference fields to).

What i try to accomplish is that the numbers are not sorted like that:

1
10
100
101
102
103
104
105
106
107

They should be sorted like that instead:

1
2
3
4
5
6
7
8
9
10

Thank you for the responses.

Link to comment
Share on other sites

I found a solution for my problem.

If anybody want to take this further, feel free to do whatever you want.

If you have any suggestions how to optimize this, please tell me.

Update: This one does not respect partial loading like pagination, there must be a better solution.

<?php namespace ProcessWire;

	class myHappyModule extends Process implements Module {

		public static function getModuleInfo() {
			return array(
				'title'     => 'My Happy Module',
				'icon'      => 'sort-amount-asc',
				'version'   => 001,
				'singular'  => true,
				'autoload'  => 'template=admin',
				);
		}

		public function init() {
			$this->addHookAfter("ProcessPageList::find", $this, "hookPageTreeFind");
		}

		public function hookPageTreeFind($event) {
			$page = $event->arguments('page');
			$selectorString = $event->arguments('selectorString');

			$children = $page->children($selectorString);

			$a = array();
			foreach ($children as $key => $value) {
				$a[$key] = array($value->name, $value);
			}

			sort($a);

			$pages = array();
			foreach ($a as $key => $value) {
				$pages[$key] = $value[1];
			}

			$sortedPages = new PageArray();
			$sortedPages->import($pages);

			$event->return = $sortedPages;
		}
	}
Link to comment
Share on other sites

@drjones, you could add a "sort_name" field (hidden perhaps) to your template to store the number with leading zeros added. You would populate this field in a hook to Pages::added in /site/ready.php

$pages->addHookAfter('added', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template == 'stick') {
        $page->setAndSave('sort_name', str_pad($page->name, 8, '0', STR_PAD_LEFT));
    }
});

Then you would use the sort settings of the parent page to sort its children by sort_name.

  • Like 2
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
 Share

  • Recently Browsing   0 members

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