Deleting multiple pages from frontend

Hi,

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:

  1. Only one page gets deleted.
  2. 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.

Here is the stripped-down code.

Template:

<form method="post">
	<?php foreach ($page->children() as $selected): ?>
		<label for="<?= $selected->uid() ?>">
			<?= $selected->uid() ?>
		</label>
		<input type="checkbox" id="<?= $selected->uid() ?>" value="<?= $selected->uid() ?>" name="removeItems[]">
	<?php endforeach ?>
	<button type="submit" name="section" value="go">Remove Item</button>
</form>

Controller:

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.

Thank you.

Hm, this is an issue that was supposed to be solved in 3.3.0, but I can still reproduce this.

Can’t get the proposed workaround to work, either.

This brutal workaround works:

$kirby->impersonate('kirby');
foreach($page->children()->listed() as $child) {
    Dir::remove($child->contentFileDirectory());
}

(Note that this method doesn’t care if the page itself or its subpages may be delete or not).

1 Like

I see, thanks for the workaround.
Two things are a bit irritating:

  1. It removes the directory even without authentification.
  2. 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 :wink:

Thanks again.

1 Like

@sennah Would you like to test if this solution also works for you: https://github.com/getkirby/kirby/issues/2323#issuecomment-558034439

Thank you, @texnixe, for the support!

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:

subpages

Now I delete the last two:

subpages_deleted

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.

Will be fixed on 3.3.2 version.

1 Like

Excellent, thank you! :grinning:

Just checked with Kirby 3.3.2-rc.1, and it seems to work just fine. Thanks again @ahmetbora and @texnixe for great support!

1 Like