How to Hook into Page Create/Update/Delete Events?

I need to ‘connect’ some of my Kirby pages to table rows in a database, and need to be able to update the rows if a page is created, updated (with a new ID) or deleted.

Example: let’s say that I am creating a Blog, where I’m only going to have a few hundred articles. So, I’m happy with having my articles as flat-file Kirby Pages. But each article might potentially have hundreds or even thousands of comments. I want to make my own commenting system, but because of the sheer amount of comments, I decide it’s best to put the comments in a database. The problem is, that each comment (in the database) is ‘connected’ to an Article page. That means, that if an Article pages changes its ID, I should be able to catch that event, and update all the comments for that article in my comments table. If an Article page is deleted, I need to immediately delete all comments for that page from the database.

To make these operations simpler, in the database I create an ‘articles’ table, containing just the Article IDs. That table is then connected to the ‘comments’ table via a foreign key, with cascading updates: if I update the Article ID in the ‘articles’ table, the database automatically updates every comment related to that article. If I delete an Article from the ‘articles’ table, the deletion also cascades, and all comments are automatically deleted. I now only have to manage the ‘articles’ table. However, I STILL need to ‘catch’ the event of when an Article Page is created, updated or deleted - so that I can create/update/delete the corresponding article record in my database table.

Am I missing something obvious? How can I do this? - all guidance truly appreciated.

Just found the Hooks Documentation - great!

The documentation states, however, that

A hook can be registered in a plugin file or in your site/config/config.php.

I would have thought, however, that the most logical place to put data-storage and manipulation code would be in the Page Model. Is there a way to use the page hooks within the Page Model?

Hooks are triggered when something happens in the Panel, and the hook events are only triggered if you use the Panel. Page Models, however, have nothing to do with the Panel, therefore: no, you can’t put them in a Page Model.

1 Like

@texnixe could we add this as a Feature Request somewhere? In a traditional MVC system, the Model should be responsible for data storage, so it would seem that in a Page Model we should be able to override or enhance CRUD operations to customise to our needs. Having to do that in ‘plugin’ or within the overall config file seems a bit counter-intuitive.

Plugins are for creating new field types, enhancing Kirbytags or making a new panel widget - that all makes sense. But having to write a plugin to tap into CRUD events for one of my page types, when we already have Page Models, is strange…

Sure, you can add a feature request on GitHub. But this will most definitely not see the light of day in Kirby 2.