drjones Posted December 1, 2017 Share Posted December 1, 2017 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 More sharing options...
Macrura Posted December 1, 2017 Share Posted December 1, 2017 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 More sharing options...
Karl_T Posted December 1, 2017 Share Posted December 1, 2017 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 More sharing options...
drjones Posted December 1, 2017 Author Share Posted December 1, 2017 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 More sharing options...
drjones Posted December 1, 2017 Author Share Posted December 1, 2017 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 More sharing options...
adrian Posted December 1, 2017 Share Posted December 1, 2017 You might try hooking after: ProcessPageAdd::executeNavJSON This is an example of using that to modify the returned list of pages: https://github.com/adrianbj/AdminRestrictBranch/blob/838858fd36a78df808b7690b4cddcb3a6c25ffa3/AdminRestrictBranch.module#L144 1 Link to comment Share on other sites More sharing options...
drjones Posted December 1, 2017 Author Share Posted December 1, 2017 4 hours ago, adrian said: You might try hooking after: ProcessPageAdd::executeNavJSON This is an example of using that to modify the returned list of pages: https://github.com/adrianbj/AdminRestrictBranch/blob/838858fd36a78df808b7690b4cddcb3a6c25ffa3/AdminRestrictBranch.module#L144 Thank you adrian. I'll look into this. Link to comment Share on other sites More sharing options...
Robin S Posted December 6, 2017 Share Posted December 6, 2017 @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. 2 Link to comment Share on other sites More sharing options...
drjones Posted December 6, 2017 Author Share Posted December 6, 2017 @Robin S, that did the trick. Thank you. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now