I am trying to delete pages from frontend. For that, I have set up a button that calls my controller and passes it the UID of the page I want to delete:
In my controller, I have tried several ways to delete the page. However I either get an Array to String conversion error or a “Call to a member function find() on null” error.
if ($kirby->request()->is('POST') && get('delete')) {
$data = [
'link' => get('link')
];
try {
$page->find($data['link'])->delete();
// I have also tried the following obviously not at the same time
$page($data['link'])->delete();
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = 'The link could not be deleted: <strong>' . $error->getMessage() . '</strong>';
else:
$alert['error'] = 'The link could not be deleted';
endif;
}
None of this make sense, you cannot find a page anywhere in the page tree just by its slug. You have to either use the UUID or the complete path to the page.
E.g $site->find('photography/trees'))
Or page('photography/trees')
Or $kirby->page('photography/trees'))
Or $kirby->page('trees', page('photography'))