Redirect after page creation

I’m trying to create a plugin with a Hook of type panel.page.create, which redirects the user to the new page’s edit form.

I tried this:

<?php
kirby()->hook('panel.page.create', function($page) {
  go($page->url());
});
?>

but it doesn’t work.

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.

Can anybody help me?

Thanks.

1 Like

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?

No, currently there is no way to use a custom js file without modifying the core and might not come either: https://github.com/getkirby/panel/issues/389

I think it would be a really nice option to have as well…
Maybe using the config file?..

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);

Here is the diff file: https://gist.githubusercontent.com/DavidUnzue/b48d96f1aa59362fc9a5/raw/265c62f11b5224d584b893c55e540f9e7cab32c1/pages.js.diff

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.

1 Like

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:

  1. Click on"Add page" and modal pops up.
  2. 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 …

You are right, so I would go for the other approach: “Create & Edit” button and redirecting.

This is now available by default in Kirby 2.2.

1 Like

Now how could I write this method to get the user to the desired page?

Kirby 2 or 3?

It’s 3

It’s not possible to redirect from page.create hook. What are you trying to achieve?

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. :see_no_evil:

So this is not about changing the slug when the page is created? The way I understand you know is that you want to react on a status change?