getkirby / ideas

This is the backlog of ideas and feature requests from the last two years. Use our new feedback platform to post your new ideas or vote on existing ideas.
https://feedback.getkirby.com
20 stars 0 forks source link

Specific hooks for page template/blueprint #368

Open afbora opened 5 years ago

afbora commented 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.


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 that

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 ❤️

afbora commented 5 years ago

You can use this repository until implemented into the Kirby core:

https://github.com/afbora/kirby-template-hooks