Kirby 2.1 Hook Usage- Creating Subpages

This seem to be the only good example of how to make a hook that creates a new page based on the adding or editing of a field in another page.

Is there any other documentation on this? Or on how to use hooks?

In my case I am thinking of using a hook to create a new page (in an adjacent folder to the one I am in) whenever a new tag is added to a tag-field, and also when it is modified etc.

Going through @texnixe example code:

  1. you are first checking if the page with that title has been already created
  2. if not, you are creating a new one

Where do you set which panel page triggers the hook, and where the new created page is placed?

In general, the hook is always triggered, but within your hook, you can of course limit the pages the hook is used on.

To create a page as a sibling of the current page, use $page->siblings()

I tried to re-work out your example to fit what I need to do, which is:

whenever I add a tag (in a field called technique) to a subpage of /artworks, create or update a subpage in /techniques accordingly.

What I have so far is this in a plugin file:

<?php

$artworks = $site->grandChildren('template', 'artworks')->children();
$tags = $artworks->pluck('technique', ',', true);

if(!empty(param('technique'))) {
  $artworks = $artworks->filter(function($result) use($tags) {
    // split tags and convert to lowercase
    $tags = array_map('str::slug', $result->technique()->split(','));
    // return the page if the filter is in the array
    if(in_array(str::slug(param('technique')), $tags)) return true;
  });
}

kirby()->hook(['panel.page.create', 'panel.page.update'], function($page) {
  foreach($tags as $tag) :
    //check if page exists
    $techniques = $site->grandChildren('template', 'techniques')->children();
    if(!$techniques->has($tag)) {
	try {

  $newPage = $techniques->create($tag, 'technique', array(
    'title' => $tag()
  ));

  echo 'The new page has been created';

} catch(Exception $e) {

  //echo "The page already exists";

 }
}
endforeach;
});

The two variables at the top give me an error saying they are undefined—I am using the same two variables in a template page and have no problem.

$site does not work in a hook, you need to define it first: https://getkirby.com/docs/developer-guide/advanced/hooks/#accessing-panel-kirby-site-and-user-in-a-hook-callback

I think I am making this all too complicated.

What I am trying to do is to replace how kirby’s tag filter works, eg kirby.site/tags:value to kirby.site/tags/value.

I tried to do this first with a route, by modifying the url—but did not manage to reach any good result. Then I thought that by doing a container page gathering as subpages all tags used in another set of subpages would do the trick, but it seems I am working against kirby’s own grain.

The reason why I am pushing for this is that I’d like to have as final urls

kirby.site/collection/authors/name
kirby.site/collection/artworks/name
kirby.site/collection/tags/name

Do you think is there any way to accomplish this in a way that the final user managing the website does not have to entry multiple times the same informations?

Is using a set of hooks viable in this case?

No, in fact, I think that a route is the right way to achieve this.

1 Like

Thanks for confirming it!

Will go back to this approach then. Was getting totally lost in possible other approaches.