What data structure for blueprint field using API image query?

I’m using an API query (custom route) to get images from elsewhere in my Kirby installation. This is for the purpose of selecting images from other sections of the CMS.

The API is basically querying pages for files using files() method like $pages->find($id)->files() from a three places {{page}}, {{page.parent}} and {{dynamically using the $id passed to the route from the blueprint}} and returns images as objects within a ‘data’ object. I then merge the three arrays and have an object in the form:

{ "products/path/to/image.png": { "content": null, "translations": null }, "products/path/to/image.png": { "content": null, "translations": null }...

The part I am interested in is the URL, and that is returned as the object key from the files() method.

I want the user to select images from the returned array with a multiselect or gallery field:

pdf_images:
label: PDF Images
type: multiselect
options: api
api:
url: "{{ site.url }}/get-pdf-images/{{ page.id }}"

Obviously I can extract the keys with get_object_vars() and then array_keys() with the route method, but was wondering if this approach is flawed, and there is a better way of doing this facilitated by other Kirby methods? I couldn’t see anything better in the docs.

Thanks in advance for your help and comments.

1 Like

As a follow up to this, my array of URLs isn’t even showing as options in the blueprint’s select. So, what data structure do I need to provide to get a selection of images as options in a blueprint field?

You can read about the expected data structure here: https://getkirby.com/docs/reference/panel/fields/select#options-via-api

Thank you, @texnixe.

I’ve got a little bit further, but I am having a problem with the {{ page.id }} part of my url value.

If I replace {{ page.id }} with an explicit id e.g. products/my-product-id, then I get results showing in the blueprint field as expected. Problem is, I need to take the id dynamically from the active blueprint currently being edited to return results relevant to that, which I thought was done using {{ page.id }}?

Do I need to use {{ page.url }} or something else? Am I doing something else wrong?

This was a bug that has now been fixed in Kirby 3.5.5+