Pages select option with full path

I want to have a “related pages” feature on some pages on my site. I figured out how to get the blueprint working so I can select pages, however they just store the page (end folder) name, not the full path. How can I store the full path so that in the template I can link to those exact pages?

This is the related pages section of the blueprint I have:

  related:
    label: Related Pages
    type: structure
    style: table
    fields:
      page:
        label: Page
        type: select
        options: pages
            
      description:
        label: Page Description
        type: text

Take a look at the Select menu’s query options.

I think you’re looking for the uri argument for the value option.

Your blueprint would look like this:

  related:
    label: Related Pages
    type: structure
    style: table
    fields:
      page:
        label: Page
        type: select
        options: pages
        value: '{{uri}}'

Basically, for each of the pages in the menu, the value attribute on the option element is set to the returned value of its uri method:

$page->uri(); # path/to/page-slug

You can use the toPage() field method to transform your field’s value into a complete page object:

<ul>
  <? foreach ( $page->related()->toStructure() as $related_item ) { ?>
    <li><?= $related_item->page()->toPage()->title() ?></li>
  <? } ?>
</ul>
1 Like

It doesn’t look like the query features of “value” or “text” or anything work in combination with options: pages. And options: query asks for another page to query from, but I don’t want to query from a single page, I want to query the whole site for the list of all pages, or actually, better yet I want to get the list of all pages and subpages within a certain section of the site.

But thanks for the template code. That will come in handy.

You can combine the page and fetch options to get an index of your entire site:

related:
  label: Related Pages
  type: structure
  style: table
  fields:
    page:
      label: Page
      type: select
      options: query
      query:
        page: /
        fetch: index
        value: '{{uri}}'

This is undocumented, but it appears to work just fine…

If you want to get, say, related pages in a specific page tree beneath a top-level page, you can use that page’s slug as the page value within the query options.

1 Like

Nice it works! Thanks.

The page input is also an option here. It references all pages and outputs the uri by default.

However the select input offers more customization and seems to work better in the structure field modal.

Thanks for your help. Much appreciated, as I am new to Kirby.

One note though. If I’m not mistaken, the last line

value: ‘{{uri}}’

should be indented further. It should be on the same level as

fetch: index

1 Like

Thanks for the hint, I have corrected it.

1 Like