I think the problem is, that after the redirect of my hook, Kirby does another redirect through the panel.js. So this second redirect is avoiding my hook to work.
Since the Panel runs mostly in the frontend, you can’t redirect from the backend. Hooks are only made to be passive and do stuff in the background, but can’t control the user flow in the Panel at this point.
Okay, I understand. And is there a way of achieving such behavior without tweaking the core? Maybe redirecting with JavaScript after clicking on the “Create page” button? But I assume you have to change the core anyway, or is there an option for custom JS in the Panel, as the one for custom CSS?
I came up with a solution for redirecting the user after adding a new page in the panel, so he/she can continue editing the contents.
My solution
This is just a tweak in the panel’s core and precisely the javascript controller under “panel/assets/js/controllers"pages.js”, so an update to the panel could or will erase the changes in the code.
I changed the lines 103-106 into this:
var pageData = $(this).serializeObject();
var pageUri = uri + '/' + pageData.uid;
PageModel.create(uri, pageData, function() {
app.main.data('current', false);
app.modal.close();
// redirect to new added page
window.location.replace('#/pages/show/' + pageUri + '/p:' + app.store.get(pageUri + '/p:', 1));
}, app.modal.alert);
I used “window.location.replace()” for redirecting, don’t know if this is the best practice. I tried to use the “PagesController.show” function, but it didn’t work.
Why am I doing this?
From a UX point of view, I don’t get the idea of a new page being added but not opened afterwards for further editing of the contents. I think everyone adding a page in the panel wants to edit it immediately, and since there is no such thing like a batch mode for creating many pages at once, I don’t see why this should be the default workflow when adding pages. If there is a reason for that, please tell me.
I think that discussion has cropped up before, can’t find the link right now. While there are certainly use cases where it might make sense to just create the page instead of editing it at once, I see your point. In fact, it would probably be even better if page creation and editing were one step, i.e. as soon as you click the add button, you are redirected to the page creation form, instead of first filling in a modal form with the title and then being redirected.
I think this issue might already have been added as feature request on Github, if not, you might like to create a new one?
I found this issue. Here, the request is to provide a “Create & Edit” button within the “Add page”-modal, in addition to the “Create” and “Cancel” buttons. @bastianallgeier already accepted this as a good idea, but as @texnixe said, using this approach, two steps have to be done for creating the new page:
Click on"Add page" and modal pops up.
Click on “Create & Edit” buttons and the editing interface for the new page opens.
whereas a unique button/step for page creation and editing, all at once, would be even better.
I’ll add a comment regarding this into the issue on GitHub.
I thought about this again, the problem I see with this idea is that for the panel to load the correct form, it needs to know what blueprint/template is intended for the page, hence the modal …
My goal is to move the selected page to another location. (If the user set the state to listed).
If the user are on that page, it causes an error. So, i want to redirect the user to a startingpoint.
I know what you’re thinking: Another guy who tries to oppress kirby.