I have a set of child pages (status listed). They can be deleted from the frontend using checkboxes. Now when several pages are being deleted at once, two problems occur:
Only one page gets deleted.
The sorting numbers of the siblings get flawed.
It does work for deleting one single page, though. In that case the sorting numbers of the siblings get adjusted correctly after the page was deleted.
return function ($page) {
if (isset($_POST['removeItems'])) {
$items = $_POST['removeItems'];
foreach ($items as $item) {
if ($page->children()->find($item)) {
$itemRemoved = $page->children()->find($item)->delete();
}
}
if (isset($itemRemoved)) {
go($page);
}
}
};
By the way: No such problems when adding multiple children at once using $page->createChild().
I couldn’t find an equivalent method like $page->deleteChild(), though.
I see, thanks for the workaround.
Two things are a bit irritating:
It removes the directory even without authentification.
It doesn’t touch the sorting numbers of the siblings but leaves “holes” in the numbering.
The “holes” are not nice but seem not to cause problems. Anyway I tried to trigger resorting using resortSiblingsAfterUnlisting() – similar to what happens behind $page->delete() (kirby/src/Cms/PageActions.php#L525). But then again the sorting numbers get flawed.
So I think I better skip checkboxes and use radio buttons instead. Until something like $page->deleteChild() comes up, removing only one page at a time will do
These are my findings after applying the suggested fix by afbora:
all selected files get deleted (not just one)
new sorting numbers get applied in sequence (no holes)
BUT the ordering of the remaining pages gets reversed
For example I add 9 pages. You can see their ordering by their slug:
Now I delete the last two:
The ordering now is reversed. When I delete more pages, the ordering reverses again.
My code used for deleting the subpages is the same as shown in the post above.