Virtual Page query

I have a virtual page on a gallery site that should list thumbnails from images stored on the parent page. The trouble is, I don’t know the parent folder, since there are multiple albums.

Structure is like this:

/albumone/thumbnails
/albumtwo/thumbnails
/albumthree/thumbnails

Trouble is, kirby thinks the page is at /thumbnails, at the top level of the site. How can i get it to see that its a subpage, and that the parent page can be dynamic. Also need it to work only if the parent page is using a template called albums, otherwise it will trigger on other pages. For example, it should not work if i go to:

/about/thumbnails

I know the pages it should not work on, those are set in stone, but there could be any number of albums, at the top level of the site, so that bit needs to be dynamic.

Is that doable?

Could you post your current code?

On a basic level this actually works, with hard coded values:

[
  'pattern' => '(:any)/thumbnails',
  'action'  => function () {
    return Page::factory([
        'slug' => 'thumbnails',
        'template' => 'thumbs',
        'parent' => page('sport'),
        'model' => 'thumbs',
        'content' => [
            'seotitle' => 'Thumbnail Page',
        ]
    ]);
  }
],

I just need page('sport') to be dynamic, based on the URL i suppose, and for the route not to work on certain pages. Ideally, only trigger this route if the thumbnails parent page is using the “album” template.

Something like this:

[
  'pattern' => '(:any)/thumbnails',
  'action'  => function ($uid) {
     if (($page = page($uid)) && $page->intendedTemplate() === 'album') {

        return Page::factory([
          'slug' => 'thumbnails',
          'template' => 'thumbs',
          'parent' => $page,
          'model' => 'thumbs',
          'content' => [
              'seotitle' => 'Thumbnail Page',
          ]
      ]);
    }
    $this->next();
  }
],

That looks promising… thanks, I will give it a shot…

Unfortunately that leads to a 404. I think its because $uid is for the thumbnail page, when actually it should be the parent of the thumbnails, and the same for the intended template, but it throws a whoops if i try and use parent() on it, or on the intended template part.

Ok, one problem is the if-statement, it has to be changed because template()/intendedTemplate() does no longer return a string like in K2, but a template object:

 if (($parent = page($uid)) && $parent->intendedTemplate()->name() === 'album') {

Another issue could arise if the parent’s template (album) doesn’t allow children.

Yep… your right… that does work. Your caveats are noted but it’s fine because it does allow children.

[
  'pattern' => '(:any)/thumbnails',
  'action'  => function ($uid) {
     if (($parent = page($uid)) && $parent->intendedTemplate()->name() === 'album') {

        return Page::factory([
          'slug' => 'thumbnails',
          'template' => 'thumbs',
          'parent' => $parent,
          'model' => 'thumbs',
          'content' => [
              'seotitle' => 'Thumbnail Page',
          ]
      ]);
    }
    $this->next();
  }
],