Jump to content

Recommended Posts

Posted

This morning I pushed a module to github which extends the page api with moving and sorting capability.

https://github.com/kixe/PageMove

 * CALLABLE (static & instance)
 * PageMove::execute($page, $parent = null, $newIndex = 0, $selector = 'all');
 * $modules->get('PageMove')->execute($page, $parent = null, $newIndex = 0, $selector = 'all');
 *
 * EXTENDED PAGE API
 * @method $page->move($parent, $newIndex = null, $selector = 'all')
 * @method $page->setIndex($newIndex, $selector = 'all') // set absolute index include=all
 * @method $page->getIndex($selector = '') // same as $page->index without argument @see getSelector()
 *
 * @method $page->moveFirst($parent = null)
 * @method $page->moveLast($parent = null)
 * @method $page->moveForward($steps = 1, $selector = 'all')
 * @method $page->moveBackwards($steps = 1, $selector = 'all')
 *
 * @property $page->moveFirst // same parent
 * @property $page->moveLast
 * @property $page->moveForward
 * @property $page->moveBackwards
 *
 * EXTENDED PAGES API
 * @method $pages->move($page, $parent = null, $newIndex = 0, $selector = 'all')
 * @method $pages->resortChildren($page, $selectorValue)
 * // same like core function $pages->sort($page, true); with capibility to change the sort condition

Have a nice weekend.

  • Like 8
Posted

Is $page->first means move page to first position? If so, wouldn't be a more descriptive name better, eg moveFirst or similar?

  • Like 1
Posted

Thanks for input. I have been searching for something really short. I am also not sure about the modules name. Any recommendation welcome. :rolleyes:

I changed the method/ property names to moveFirst and moveLast.

  • Like 1
Posted
53 minutes ago, LostKobrakai said:

Just so you don't miss them; there are sorting functions in the dev version of 3.0:

Of course I missed them. Thanks for the hint.

  • Like 1
Posted (edited)

@LostKobrakai

I took a look in the core sorting functions. Unfortunately it doesn't work as expected and the $pages API reference is faulty and should be updated:

// set $page to have sort=5, moving any 5+ sort pages ahead
$pages->sort($page, 5); // works as expected

// same as above using alternate syntax
$page->sort = 5; // WILL NOT moving any 5+ sort pages ahead it just changes the sort value. Duplicates possible
$pages->sort($page); // WILL NOT set anything. This is a getter not a setter. Similar to $page->sort

In the $page API reference, $page->sort is described as follow which is faulty too.

Quote

Sort order of this page relative to siblings (applicable when manual sorting is used).

Use $page->index instead to get expected/ described result. But you cannot use $page->index as a setter!


Currently the only (nearly)  working solution needs the following 2 steps

$pages->sort($page->parent, true); // re-build sort values for children of $page, removing duplicates and gaps
$pages->sort($page, 5); // sort +=1 for all pages above

In this example you will get an unexpected result If the sort position related to the siblings was 4 before. There will be no change, because there is a gap at sort position 4 now.


What I would expect using this function

If I set sort I would expect a zero based position related to the page siblings.


Example

$pages->sort($page, 2);

3 page siblings having the sort values 4, 7, and 11. If I assign a sort value 2 to the page with the former sort value 7 I would expect the page moves to the end (last index number under 3 siblings), but currently it moves in the opposit direction and will be the first one related to its 2 siblings.

Nice to have in the future
(already available via PageMove module)

// move page to the first or last position related to its siblings
$page->moveFirst;
$page->moveLast;
$page->moveFirst($newParent = null);
$page->moveLast($newParent = null);

// move the page to an index position relative to its siblings, optionally change the parent too in one step
$page->move($parent, $newIndex = null, $selector = 'all')

// or simply
$page->setIndex($newIndex, $selector = 'all');


Expecting a core update related to this I will not push the module to the modules directory. Feel free to pull it from github directly until this is available from core.

 

 



 

Edited by kixe
module update 1.0.2
  • Like 3
Posted

$page->sort always worked this way, which is why most people simply avoided changing it most of the time. It might need some clarification now that there is a function which does prevent duplicate sort values.

13 minutes ago, kixe said:

What I would expect using this function

If I set sort I would expect a zero based position related to the page siblings.

Rebuilding all sort values is a waist of db computation. Imagine you've 10000 siblings and you reorder just the 9999 to be the last one. If there are gaps in the first siblings sort values it'll rebuild all 10000 instead of just a few pages.

16 minutes ago, kixe said:

Example


$pages->sort($page, 2);

3 page siblings having the sort values 4, 7, and 11. If I assign a sort value 2 to the page with the former sort value 7 I would expect the page moves to the end (last index number under 3 siblings), but currently it moves in the opposit direction and will be the first one related to its 2 siblings.

I think you expect to set the new index of the page not the sort value, which in ProcessWire are currently different things. I see that it might be confusing, but mostly because people rarely dealt with that issue by now as sorting was mostly done via the GUI / modules.

It would be great if you could formulate those issue you had in a github issue, so that ryan can improve on them. 

  • Like 2
Posted
19 hours ago, LostKobrakai said:

Rebuilding all sort values is a waist of db computation. Imagine you've 10000 siblings and you reorder just the 9999 to be the last one. If there are gaps in the first siblings sort values it'll rebuild all 10000 instead of just a few pages.

I completely agree. I don't mind about the absolute sort value, but the page index should be in the right place. In my example it is enough if the page with previous sort value 7 get the index 2, which is the fact if a sort value of 12 is assigned (one more than 11)


For handling pages the absolute sort value is not of interest it does say nothing, the index position is the important value.
So it would be nice in addition to get the index via $page->index  having a setting method.

19 hours ago, LostKobrakai said:

It would be great if you could formulate those issue you had in a github issue, so that ryan can improve on them. 

Of course I will do that.
https://github.com/processwire/processwire-issues/issues/225

  • Like 1
Posted

I pushed a revision 1.0.2 of the module with extended capability and more delimited from core functions (still using them if possible).

  • Like 1

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
×
×
  • Create New...