Skip to content

Pages::sort() unexpected result #225

@kixe

Description

@kixe

Short description of the issue

Unexpected result using $pages->sort($page, $newSort);

Expected behavior

If I set a sort value of a page I would expect a setting of a zero based position relativ to the page siblings independent from the absolute sort value.

For handling pages the absolute sort value is not of interest, the index (position) is the important value and therefore the index position should be changed using this function.

Actual behavior

Example 1

$page->sort // int 4
$page->index // int 0
$page->siblings()->each('sort'); // array(4, 7, 11);
$pages->sort($page, 2); // int 1
$page->index // int 0 (instead of expected 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.
To get the expected result the sort value could be anything bigger than 12.

Example 2

$page->sort // int 4
$page->index // int 4
$page->siblings()->each('sort'); // array(0, 1, 2, 3, 4, 5, 6);
$pages->sort($page, 5); // int 3
$page->index // int 4 (instead of expected 5)

There will be no noticeable change, because there is a gap at sort position 4 now. To get the expected result the sort value should be set to 6 while pushing the page with sort 6 to sort 7.

Note: Currently there is no core function to set $page->index

Optional: Suggestion for a possible fix

some input/ inspiration
https://processwire.com/talk/topic/15908-page-sorting-and-moving-via-api/
https://github.com/kixe/PageSortMove

Setup/Environment

  • ProcessWire version: 3.0.57

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions