medienbaecker / kirby-modules

Plugin for modular Kirby websites
MIT License
73 stars 7 forks source link

Prevent redirection to modules container on page deletion #4

Closed medienbaecker closed 4 years ago

medienbaecker commented 4 years ago

When deleting a module you get redirected to the (otherwise invisible) modules container.

medienbaecker commented 4 years ago

Redirection in a panel hook is not possible (yet) since hooks can only interact via throwing Exceptions.

I'll try putting the redirection in a custom field that will be present on the modules container.

medienbaecker commented 4 years ago

Redirecting in a field doesn't seem trivial either.

For now there should be a blueprint for the modules container with an info field. It's not ideal, but better than displaying the default blueprint.

nilshoerrmann commented 4 years ago

My guess is you'd like to redirect to the module's grandparent page, the one containing the main content. I'm also assuming that Kirby is always redirecting to the direct parent after deleting a page. So have you tried changing the modules parent from the module container to the actual content page before deletion?

medienbaecker commented 4 years ago

@nilshoerrmann That's a great idea and I didn't think of this at all. Unfortunately—if I did everything right—it doesn't work either.

I added this hook:

'page.delete:before' => function ($page) {
    if($page->parent()->intendedTemplate() == "modules") {
        $oldPageFolder = basename($page->contentFileDirectory());
        $newPageFolder = $page->parent()->parent()->contentFileDirectory() . DIRECTORY_SEPARATOR . $oldPageFolder;
        Dir::move($oldPageFolder, $newPageFolder);
    }
}

But it still redirects to the modules container page. I think the variables are set before the :before hook or something like that?

nilshoerrmann commented 4 years ago

Okay, I'm only guessing, but maybe it's possible to manipulate the $page object that is passed to the hook. So maybe you could just make the page "think" that it has a different parent without actually moving folders?

medienbaecker commented 4 years ago

I spent hours trying to get this redirection to work—no chance. For now I added a link to the parent page in the modules blueprint. This way you can at least get to the parent page with one click.

nilshoerrmann commented 4 years ago

Maybe you could ask Bastian, if it's possible at all. Does Kirby allow for custom routes for the panel maybe?

medienbaecker commented 4 years ago

image

Unfortunately it seems like there's no solution for this.

medienbaecker commented 4 years ago

I think I found a solution. It's quite hacky but it works:

The modules plugin now includes a simple modules_redirect field. I pass $this->model()->parent()->panelUrl() to it and redirect to this url on render.

You can check it out in the branch feature/modules_redirect.

nilshoerrmann commented 4 years ago

Hacky – but clever! 👍