Panel templateChange hook


#1

We have a hook called panel.page.move and it’s triggered if a page (folder) is renamed. We also have the hook panel.page.update which is triggered when a page is updated.

I’m missing a hook called something like panel.page.templateChange which should be triggered if a template is changed (file is renamed).

At first it seems like a good idea to only have panel.page.update because changing a template is a kind of an update. The difference is that with a normal update it changes the content inside the file. When changing the template it rename the file. It’s two different operations.

We also have great methods that reflects the hooks:

But there is no function that I know of that renames the file on a template switch like $page->changeTemplate('from', 'to', $lang).

Plugins like https://github.com/jenstornell/kirby-sync and https://github.com/jenstornell/kirby-revisions would benefit from a hook like that.


#2

Hm, the issue is probably that the changeTemplate() method would then call two hooks, one when renaming the text file, and a second when updating the page content array?


#3

I’m not sure how you mean. In the panel you can change the template or save the page, you can’t do both at the same time.

Now it works like this:

  • Change template = panel.page.update
  • Save page = panel.page.update

My suggestion:

  • Change template = panel.page.changeTemplate
  • Save page = panel.page.update

Alternative suggestion

Because panel.page.update is triggered for two tasks and to not make a breaking change it would be possible to keep panel.page.update like it is but also make two subhooks available as an alternative:

  • Change template = panel.page.changeTemplate
  • Save page = panel.page.save

But as I wrote, keep panel.page.update as well.


#4

Well, what I meant was that if you have a look at the source code where the template changing happens, you will see that there are in fact two actions in it: first f::move to rename the file, then $page->update() to update the content.


#5

I’ve tested it some more now in the panel.

If I write some content that I don’t save and then refresh the browser it says:

You have unsaved changes!

So far so good. It does not save my content because I did not press “Save”. I change my template, still with unsaved content.

Now it looks like it saved the data as well because the green smiley showed up and the message is gone. By looking at the title afterwards it seems to have saved the old content again, not the session/changed content. :upside_down_face:

My guess is that it destroys the session on template change and that the page updates to how the page was before the refresh, only to keep the page object updated.

In a plugin I’m working on I renamed a content textfile and then tried to get the new page template with page('a-page')->template() but it always showed me the old page object (template) before I renamed the file. I guess it uses some kind of cache. A page update triggered by the plugin there would probably solve the problem, just to keep the page object up to date.

(Many guesses in this post)

Update

Personally I hope Kirby handles this a bit more elegantly in the future. Like keeping the session all the time until I press save and on change template, only change the template, not save the page.