Open afbora opened 5 years ago
First of all, I have 65 templates in my current project. I can also say that the number will increase.
At first I was using hooks like this:
return [ 'hooks' => [ 'page.update:after' => function ($newPage, $oldPage) { $template = $newPage->intendedTemplate(); switch($template) { case "product": // TODO: About price // TODO: About dates // TODO: About attributes // TODO: About images break; case "order": // TODO: Something break; case "category": // TODO: Something break; case "post": // TODO: Something break; ... ... ... } } ] ]
As you can see, it's not handy. It is very difficult to read, update and manage. When things started getting messy, I created a hooks class.
Considering that the pages in Kirby are the most elements, it didn't make sense to collect all the page hooks under one method.
I thought about ways to make it easier. The most important thing separating the pages from each other, page templates/blueprints came to mind.
I would like to give an example of two new uses:
1. Hooks with template name: page.TEMPLATE.update:after or something like that
page.TEMPLATE.update:after
return [ 'hooks' => [ 'page.project.update:after' => function ($newPage, $oldPage) { // your code goes here } ] ]
2. Assigning an class (Advanced)
class PageProjectUpdateAfter extends \Kirby\Cms\NewHookClass { public function handle($newPage, $oldPage) { // TODO } } return [ 'hooks' => [ 'page.project.update:after' => 'PageProjectUpdateAfter' ] ]
Thank you for listening to me again. Stay with Kirby ❤️
You can use this repository until implemented into the Kirby core:
https://github.com/afbora/kirby-template-hooks
First of all, I have 65 templates in my current project. I can also say that the number will increase.
At first I was using hooks like this:
As you can see, it's not handy. It is very difficult to read, update and manage. When things started getting messy, I created a hooks class.
Considering that the pages in Kirby are the most elements, it didn't make sense to collect all the page hooks under one method.
I thought about ways to make it easier. The most important thing separating the pages from each other, page templates/blueprints came to mind.
Ideas to solve
I would like to give an example of two new uses:
1. Hooks with template name:
page.TEMPLATE.update:after
or something like that2. Assigning an class (Advanced)
Thank you for listening to me again. Stay with Kirby ❤️