cms
cms copied to clipboard
Error when deleting page that has children
Bug Description
When you delete a page that has children you get an error "Root page cannot have children". When you delete the children individually first you can bypass this bug.
How to Reproduce
- Add a page
- Give the page one or more children
- Delete that parent page
Or install Peak, it comes with pages featured in the example below.
statamic new site studio1902/statamic-peak
Extra Detail
Possibly related to #3791 https://user-images.githubusercontent.com/69107412/136973617-43fb2fec-44e3-4460-8fdd-91e670470ceb.mov
Environment
Statamic 3.2.14 Pro Laravel 8.63.0 PHP 7.4.13 No addons installed
Install method (choose one):
- Starter kit Peak
The error seems to be because $entry->delete() is being called on all three entries, but first on the top level parent. Because of this the child pages are being 'moved up' to the parent. Might be fixed by making sure the entries are sent in the order of the reverse tree (children and then up). This way there wouldn't be any descendants for the parent pages by the time they get deleted.
I'm still not 100% sure why this would try to move them under the root though. https://github.com/statamic/cms/blob/3.2/src/Entries/Entry.php#L174
I'm still not 100% sure why this would try to move them under the root though.
Passing null as the second argument to move will move it to the root.
Thanks Jason, that makes sense. I have found a fix for this issue, but found another bug (?). It's mentioned in the PR, if you confirm that is a bug I'll make a new issue for it.
Hi!
This error still occurs in v3.3.17.
Statamic 3.3.17 Pro
Laravel 8.83.18
PHP 8.0.16
doublethreedigital/duplicator 2.3.1
optimoapps/statamic-bard-text-align 1.0.2
spatie/statamic-responsive-images 2.12.4
statamic/ssg 1.0.1
[2022-07-08 08:54:47] local.ERROR: Root page cannot have children {"userId":"790b1007-849c-4144-9e78-237388f0c605","exception":"[object] (Exception(code: 0): Root page cannot have children at /var/www/html/cms/vendor/statamic/cms/src/Structures/Tree.php:297)
[stacktrace]
#0 /var/www/html/cms/vendor/statamic/cms/src/Entries/Entry.php(169): Statamic\\Structures\\Tree->move('96cae355-3c82-4...', NULL)
#1 /var/www/html/cms/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(245): Statamic\\Entries\\Entry->Statamic\\Entries\\{closure}(Object(Statamic\\Structures\\Page), 0)
#2 /var/www/html/cms/vendor/statamic/cms/src/Entries/Entry.php(170): Illuminate\\Support\\Collection->each(Object(Closure))
#3 /var/www/html/cms/vendor/laravel/framework/src/Illuminate/Support/helpers.php(263): Statamic\\Entries\\Entry->Statamic\\Entries\\{closure}(Object(Statamic\\Structures\\CollectionTree))
#4 /var/www/html/cms/vendor/statamic/cms/src/Entries/Entry.php(172): tap(Object(Statamic\\Structures\\CollectionTree), Object(Closure))
#5 /var/www/html/cms/vendor/laravel/framework/src/Illuminate/Support/helpers.php(263): Statamic\\Entries\\Entry->Statamic\\Entries\\{closure}(Object(Statamic\\Structures\\CollectionStructure))
#6 /var/www/html/cms/vendor/statamic/cms/src/Entries/Entry.php(173): tap(Object(Statamic\\Structures\\CollectionStructure), Object(Closure))
#7 /var/www/html/cms/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php(60): Statamic\\Entries\\Entry->delete()
#8 /var/www/html/cms/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(245): Illuminate\\Support\\HigherOrderCollectionProxy->Illuminate\\Support\\{closure}(Object(Statamic\\Entries\\Entry), 0)
#9 /var/www/html/cms/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php(61): Illuminate\\Support\\Collection->each(Object(Closure))
#10 /var/www/html/cms/vendor/statamic/cms/src/Actions/Delete.php(55): Illuminate\\Support\\HigherOrderCollectionProxy->__call('delete', Array)
[shortened]