Blueprint select field queries for more than one page

Hi,
is there a way in a select field to query the children of more than one page? The following syntax does not work:

productlink:
label: Products index link
type: select
options: query
query:
page:
- products
- press
fetch: children
value: '{{uid}}'
text: '{{title}}'
flip: false

Do I have to write a custom select field? Are there any pointers on how to do something like that?
Thanks.
Joern

Hi,
any suggestion on this?

Thanks
Patrik

The best way to go about that would be to use dynamic options via a json API: https://getkirby.com/docs/cheatsheet/panel-fields/select

E.g. json.php template

<?php
header('Content-type: application/json; charset=utf-8');

$collection1 = page('projects')->children();
$collection2 = page('products')->children();
$newCollection = $collection1->merge($collection2);

foreach($newCollection as $p) {
	$json[(string)$p->uid()] = (string)$p->title();
}

echo json_encode($json);

?>

Make sure that the children of the two main pages do not have the same uid.

I see how this works. I get the logic of it, but I’m missing some key info to set this up. Okay. You’re dynamically generating a json file. Cool, but where does this json file live (what’s the URL)? You’re creating a ‘json.php’ template. So does this live in the site/templates folder? Does this mean this json file is a page? I’m almost there. I think I just need a step by step.

You can generate the json file on the fly via a route that returns the json. So the URL would be the URL of the route, you don’t necessarily need a template.

If you want to use a template, then you need to create a content page and the file will live at the URL of the page.

Thanks for the quick reply. I did create the page and when I visit that page it indeed spits out the json formatted data. But this page is not a .json file so in the select field options I did not provide a .json extension. It didn’t populate the select field. Seems like creating it via route (however that’s done) would be more elegant, but in the end do you need to supply a path to a .json file or can it be something like /api/select-source

Have you made sure that your URL is correct? It works perfectly in my installation.

E.g.

Which Kirby version are you using?

Thanks for your time on this. I’m using 2.3.2

Does it have to be a absolute/full URL or can it be /folder/uri

No, it has to be a full URL, a path does not work.

The full URL did not work locally. I have a virtual host (domainname.dev) setup. I don’t access the local site using http://localhost… I figured I upload everything to a remote server, on a subdomain I setup. I does work on the remote side. Hmmmm…

So as a resource let me spell this out for people like me.

If you wish to use the select field and dynamically populate the options using the JSON API here’s the recommended approach:

Setup a route, not an invisible page. To do this open the site/config/config.php file. This is where would write your PHP to dynamically build your JSON file. I’m using texnixe’s code sample below.

You’ll see there’s some extra business that relates to setting up the route. That is explained in the docs here. Notice the pattern value? That’s the URL to the json file. You can make this whatever path you want.

c::set('routes', array(
  array(
    'pattern' => 'api/my-dyno-options.json',
    'action'  => function() {
        header('Content-type: application/json; charset=utf-8');

        $collection1 = page('blog-articles')->children();
        $collection2 = page('news-articles')->children();
        $newCollection = $collection1->merge($collection2);

        foreach($newCollection as $p) {
        $json[(string)$p->uid()] = (string)$p->title();
        }

        echo json_encode($json);
    }
  )
));

Now that you created your route go to the URL to ensure the json is being generated. Copy and paste that URL into the options for your select field. Something like this below.

my_featured_article:
    label: Featured article
    type: select
    options: http://mydomain.com/api/my-dyno-options.json

Note: The select option value has to be a absolute/full URL.

It’s kind of a drag that the path has to be a full URL because, like many of us, I work local then switch to production. We’ll have to remember to change these URLs.

Another note to mention is the issue I ran into (explained above). For some reason my virtualhost setup which uses something like mydomain.dev instead of localhost/mydomain/ doesn’t work. I don’t know if this is a Kirby thing or the software that allows me to setup a local environment.

4 Likes

Thanks for sharing your final result, @thewebprojects :slight_smile:

Is there some server setting or PHP module that needs to installed for this method to work? The reason I ask is that this setup, as outlined above, worked absolutely fine on my host. The project then required a change of web host. The select fields no longer pull the data from the json file. Note: The source URL (address of the json file) is correct. That has not change nor does it need to. When I visit the URL (set in the config) it generates the json.

I am not aware of any modules required for this feature. Is the json file on the same domain?

Yes. It’s on the same domain. I don’t know if this matters but the json file is minified (e.g. no line breaks, etc.). Does that matter? I don’t recall if it was like that when it was working on my host.

That shouldn’t make any difference, but didn’t you say you use a route, anyway?

Yes. It is a route.

Thanks for the reply. I’ll dig and report my findings/solution.

@thewebprojects I also was trying to fix this and found the “relative url issue” too annoying. So I used rasteiner/controlledlist. Works nice :slight_smile:

Thanks. I’ll give this a look.

Update: since 2.5 you can we can use a relative path to the source/json file. Oh joy.