How to query a structure field to checkboxes

I have a structure field on one blueprint like this:

fields:
  services:
    label: Services
    type: structure
    fields:
      platform:
        label: Service
        type: text

and want to query it in another blueprint as a checkbox list. What do i have to add in the query to get the ‘services’ fields as checkboxes?

fields:
  category:
    label: Category
    type: checkboxes
    options: query
    query: site.children.template('services')

In which page is that structure field?

It is documented here: https://getkirby.com/docs/reference/panel/fields/checkboxes#options-from-other-fields under “Options from structure field”

The structure field is on a different page than the one where i want to create the checkbox list

Well, that’s why I was asking, you have to indicate the page:

query: 
    fetch: site.find('the-page-with-the-structure-field').services.toStructure
    text: "{{ structureItem.platform }}"
    value: "{{ structureItem.platform }}"

It’s weird. site.find('my-page') does not work with or without the additional .services.toStructure. doesn’t matter what of my pages i put in there. site.children.template('my-template') gives me the pages i want to fetch. But when I add .services.toStructure like this:

query: 
    fetch: site.children.template('services').services.toStructure
    text: "{{ structureItem.platform }}"
    value: "{{ structureItem.platform }}"

it does not work either… Is it possible that site.find() simply does not work here and is it possible that i cannot fetch multiple templates?

You want to fetch a field, so you need a single page, but site.children.template('whatever') gives you a (filtered) collection (even if only one page with that template exists). And you just can’t call a field on a collection.

Is your subfield in the structure really called platform?

This here refers to a field that is present in your structure item, in this case platform but you have to change this depending on your field names.

yes it is called platform.

Well, I do know that it works, for example, if I put this into any blueprint in a Starterkit:

fields:
  category:
    label: Category
    type: checkboxes
    options: query
    query:
        fetch: site.find('about').social.toStructure
        text: "{{ structureItem.platform }}"
        value: "{{ structureItem.platform }}"

It will query the about’s page social field.

And render this:

1 Like

I am having a similar problem and have tried to simplify the blueprint as much as possible but am still having issues. I have a series of pages that use the research-nugget blueprint. For testing, I have put the structure field in the same blueprint as the one I want to query. It queries that structure field on sibling pages. Here is part of my research-nugget.yml blueprint:

fields:

  text:
    type: textarea


  publications:
    label: Related publications
    type: multiselect
    options: query
    query:
      # fetch: page.siblings.template("research-nugget")
      # text: "{{ page.title }}"
      # value: "{{ page.uid }}"
      fetch: page.siblings.template("research-nugget").papers.toStructure
      text: "{{ structureItem.title }}"
      value: "{{ structureItem.year }}"

  papers:
    type: structure
    fields:
      title:
        type: text
      year:
        type: number

In the code above, if I query the the sibling page itself (the part that is commented out) it works fine. But when I try to query the structure field, this is the error I get:

I am running Kirby 3.5. I have tried to troubleshoot this for awhile, reducing it to its simplest essence, and still have no luck. What am I missing?

Thanks,

Keith

Hi there,

Any follow up on this?

I have exactly the same issue with multiselect field query on 3.5.3.1

Any ideas on what’s happening?

Cheers

The problem in this thread is that people tried to fetch the field from a collection, which is just not possible, because it is wrong syntax (ie. that code wouldn’t work in a template either).

What you would have to do (if that’s what you want to do), is create a single structure object from these pages and then use that in the query.

Thank you and you’re right, I think I’ve managed to figure out.

:pray:

I want to do similar thing, but display structure field as structure field. The goal is to show options for product, so that client could add new fields himself from panel.

I imagine something like this, but nothing works:

          foptions:
            type: structure
            fields: query
            query:
              fetch: site.find('catalog').foptions.toStructure
              fields:
                title:
                  type: text
                  default: "{{ structureItem.title }}"
                measure:
                  type: text
                  default: "{{ structureItem.measure }}"

Or in other words, the goal is to repeat structure field on other pages, so that there is pre-populated structure field on every product blueprint page.

i do not think structures support dynamic fields via a query via yaml.

but i guess you could solve that via a programmable blueprint

The only way to achieve this would be via programmable blueprints, as @bnomei just added. This only works if the fields are defined in a fixed page or in the site.