Issue with Draft pages and Router

I have a route that is creating subcategory pages dynamically from a Tag field ($category is an actual page that exists)

  'pattern' => 'knowledge-base/(:any)/(:any)',
  'action'  => function ($category, $subcategory) {
      if ($page = page('knowledge-base/' . $category . '/' . $subcategory)) {
          return $page;
      } else {
          return Page::factory([
              'slug'     => $subcategory,
              'template' => 'subcategory',
              'model'    => 'subcategory',
              'parent'   => page('knowledge-base/' . $category),
              'content'  => [
                  'title' => urldecode(ucfirst($subcategory)),
              ]
          ]);
      }
  }
],

This is all working fine, but when I set an article page to ‘Draft’ it is returning the ‘else’ side of this argument instead of the page itself. I guess it’s something to do with the token that’s added to Draft pages? I changed the pattern to 'pattern' => 'knowledge-base/(:any)/(:alpha)' to test, which works unless the subcategory has a space in it then the URL is encoded with a ‘+’ and returns an error. I’m not sure how to return a Draft page in this route.

Try

$page = kirby()->page('knowledge-base/' . $category . '/' . $subcategory);

Great, thanks! What’s actually happening here, so I understand?

The page() helper only finds published pages, not drafts, whereas kirby()->page() includes drafts bei default.

2 Likes