Here is the scenario: let’s say that we have a Blog, where at the end of each Article we can have an optional list of ‘Related Articles’. The ‘Related Articles’ are implemented in the Panel as a TAGS field: the field lists other articles’ titles, but stores their ID.
Now, whenever an existing article is DELETED from the site, we must do a check to see whether that article is listed as a ‘related article’ in any article pages - and if so, that reference must be deleted, before the actual article itself is deleted - akin to a ‘cascade delete’ on a database. So, how do we do this the most ‘elegant, Kirby 3’ way? More specifically: do we use a hook or a model?
The obvious way to implement this would be to write a function in the 'page.delete:before'
hook. But that hook would trigger on the deletion of every page, not just article pages, so we’d have to start the function by doing a check - not very optimal.
The more “OOP”-ish way to handle CRUD functions on data is to put all those functions in the model - e.g., create an ArticlePage
model class that extends the parent’s “delete” method to add additional checking operations it will do on ‘delete’. That ‘feels’ like the ‘proper’ way to do it, as the method would then only be called when Article pages are deleted - not all pages - and keeps all data-management operations inside the model. However, there is very little documentation in the Guide about extending the default CRUD functions, and even a warning about being careful when overriding the Page class - so I’m not sure whether this is actually the best way to go about it…
Any guidance from the Kirby Gods would be appreciated!