Hi,
I try in a hook to programmatically delete pages which has also subpages and delete them all (page & subpages):
foreach ( $proposals as $proposal ) :
$this->impersonate('kirby');
$proposal->delete( true );
endforeach;
But pages are not deleted :-/
Is it allowed in Kirby to delete a page programmatically if it has subpages?
Hm, at least some of the pages should get deleted though (if they are not drafts). Do you get any error messages? Or have you set deleting of pages too false? Which hook are you using?
What do you mean by âif they are not draftsâ?
We canât delete drafts pages programmatically?
I donât have error message and all my delete are set to true.
I use the âpage.duplicate.afterâ hook
And the purpose is to delete the subpages once the page is duplicated? Could you please post the complete hook?
And the purpose is to delete the subpages once the page is duplicated?
Yes, I want to delete subpages even if the user set the âCopy pagesâ toggle to âOnâ when duplicating the page.
This is my hook:
'page.duplicate:after' => function ( $duplicatePage ) {
if ( $duplicatePage->template() == 'partnership' ) :
$campaigns = null;
$campaigns = $duplicatePage->childrenAndDrafts()->filterBy( 'template', 'campaign' );
foreach ( $campaigns as $campaign ) :
// delete proposals pages
$proposals = $campaign->childrenAndDrafts()->filterBy( 'template', 'proposal' );
foreach ( $proposals as $proposal ) :
$this->impersonate('kirby');
$proposal->delete( true );
endforeach;
endforeach;
endif;
},
The pages hierarchy is: partnership > campaign > proposal > subpages > sub-subpages
subpages and sub-subpages are drafts pages
I also have âpartnership > campaign > evaluatorâ pages and I have no problem to delete them in the hook
Do these templates partnership.php
, campaign.php
and proposal.php
actually exist? If they do not exist, you have to use intendedTemplate
instead.
Hm, I tried to set up a similar structure and in my local setup, the subpages are deleted, while the campaign pages are preserved (using intendedTemplate
, because I couldnât be bothered to set up new templates).
While there are those issues quoted by @ahmetbora with deleting pages in hooks, at least some pages should actually get deleted.
Could you provide the project or a stripped down version with only the relevant parts for testing?
Ok, I will send you the project as soon as possible, I must leave my house right now.
May I send you a link to download the zip by email? If yes which email?
sonja[at]getkirby[dot]com ?
Just before to leave, I tried to delete âproposalâ in the âparnershipâ template and it works, proposal and subpages are deleted.
partnership.php
$campaigns = $page->childrenAndDrafts()->filterBy( 'template', 'campaign' );
foreach ($campaigns as $campaign) {
$proposals = $campaign->childrenAndDrafts()->filterBy( 'template', 'proposal' );
foreach ( $proposals as $proposal ) :
try {
$kirby->impersonate('kirby');
$proposal->delete( true );
} catch(Exception $e) {
echo $e->getMessage();
}
endforeach;
}
Hi Sonja, you should receive my application in the next minutes âŚ
Hey Gilles, just to let you know: I havenât received anything yet.
Mhmm weird
I 've resent the email âŚ
texnixe
October 6, 2019, 12:21pm
17
Hm, the only way to reliably remove everything Iâve found, is using low level methods:
'page.duplicate:after' => function ( $duplicatePage ) {
if ( $duplicatePage->template()->name() === 'campaign' ) {
kirby()->impersonate('kirby');
foreach ($duplicatePage->childrenAndDrafts() as $child) {
Dir::remove($child->contentFileDirectory());
}
}
},
Thank you Sonja for looking at my code, I will use your solution.
Should I create an issue on GitHub?
Yes, please create an issue referencing the other issues.
Iâve also created a feature idea to prevent copying of the tree rather then having to delete those pages afterwards: