I’m new to Kirby and working on a research-focused site where I need to have a many-to-many-style relationship between authors and articles, like this:
I’ve come up with a solution (described below), but my question is basically: is this a good solution? Are there pitfalls/issues I’m not seeing? Is it cool to use uuids to reference things like this?
Here’s what I did:
- Articles have a multiselect field that displays the author names as options, and saves their uuid to the article
- Author pages display articles of posts if they contain the author’s uuid
- When an author is deleted, it triggers a hook (see below) to remove the author’s uuid from all articles
'page.delete:after' => function ($page) {
if ($page->template() == "author") {
$articles = page('articles')->children()->filterBy('auths', $page->uuid(), ',');
foreach ($articles as $article) {
$auths = $article->auths()->split(',');
if (($key = array_search($page->uuid(), $auths)) !== false) {
unset($auths[$key]);
}
$updatedAuths = implode(',',$auths);
try {
$newPage = page($article->uuid())->update([
'auths' => $updatedAuths
]);
error_log('The author has been removed.');
} catch(Exception $e) {
$outputMessage = "From author hook:" . $e->getMessage();
error_log($outputMessage);
}
}
}
}