Moving page - resort old siblings

Hi there,
I have an Issue when I’m moving a page programmaticly, the sorting number of the old siblings stay the same and leave a “gap” in the sorting numbers. This wouldn’t be a great issue if everything is handled in the panel, but since there are also pages created programmaticly that should be appended to the old parent this can result in duplicated sorting numbers.

In my case old blogarticles get moved to an archive and new references create an automated new blogarticle.

Is there any way to resort the children of a page after on child is moved? Maybe something similar to $page->resortSiblingsAfterUnlisting(); in the PageAction.php?

I also tried to iterate through all the old siblings and change the sorting number, but it stops after the first sibling…

$kirby->impersonate('kirby');
try {
  $siblings = $page->siblings();
  $i = 1;
  foreach($siblings as $sib){
    $n = $i++;
    $sib->changeNum($n);
  }
}

Another strange thing is that I already created an archive a couple years ago with the same functions, but at that time the move action wasn’t available. Back then I copied the page and deleted the original, that way the sorting numbers where updated. This doesn’t work either anymore. So maybe somebody has any idea how to solve this.

Thanks in advance

cheers
tom

changeNum() does not resort the siblings, while changeSort() does.

You could probably handle this in a page.move:after hook, where you have access to the oldPage and therefore the old siblings.

Thanks for hint with the hook, I couldn’t find page.move hooks in the docs, maybe they’re missing, but I give it try. I’ll guess it would be:

'page.move:after' => function($oldPage, $newPage){
  $siblings = $oldPage->siblings()->listed();
  ...
}

As always, @texnixe pointet in the right direction. The page.move:afterdid the trick. Maybe at some point this could be added to the docs. But for now my solutions looks like this:

  'page.move:after' => function($oldPage, $newPage){
    if($oldSiblings = $oldPage->siblings()->listed()){
      $oldPage->changeSort($oldSiblings->count());
    }
    if($newSiblings = $newPage->siblings()->listed()){
      $newPage->changeSort($newSiblings->count());
    }
  },

The moved page now gets sorted in at the end of it’s new parent.

So again thank you Sonja :slight_smile:

cheers
tom