Query fields from a file template

I have a structure field on a file template. I would like to dynamically access that field from the page that holds that file. Is this possible?

I can make a select field that dynamically accesses the file.

  style:
    label: Font Style
    type: select
    width: 1/3
    options:
      type: query
      query: page.files.template("fontloader")

But it falls apart when I try to access any of the fields attached to these files.

Hi, welcome to the forum. Could you please post your code?

Btw. there is also a files field, which is like a select but specialized in files: Files | Kirby CMS

Code to retrieve data from those fields is the same: Files | Kirby CMS

When a file is added to the page (in this case variable.woff for example) in the files blueprint (fontloader.yml) you fill some fields in a structure. Below is one example from the fontloader blueprint.

fields:
  style:
    label: Font Style
    type: structure
    width: 2/3
    fields:
      stylename:
        type: text
      instance:
        type: text

On the main page where the file is added I am editing the heading block site/blueprints/blocks/heading.yml I want to be able to create a select field that will dynamically query all the options from all the files that were uploaded to that page using the fontloader.yml template. Here’s the relevant part from that heading.yml

  style:
    label: Font Style
    type: select
    options:
      type: query
      query: page.files.template("fontloader")

I can get the block to create a select for all the files with that template with the above code.
Where I’m stuck is when I try to get that select to let me choose specific fields from the fontloader.yml I’ve tried a few variations of code below.

  style:
    label: Font Style
    type: select
    options:
      type: query
      query: page.files.template("fontloader").style.toStructure
         text: "{{ item.stylename }}"
         value: "{{ item.instance }}"

But I can’t get it to work. So back to the question. Is it possible to dynamically access these fields? It’s more than just selecting a file, but selecting specific information attached to that file.

Ok, so this is not about frontend code, but getting information inside the panel.

But this query doesn’t make any sense, because you cannot call the field name on a collection of files. That would only be possible on a single file.

So what you would need here is a custom method that fetches the information from all files into a single structure/array, and take it from there.

1 Like

Thank you for clarifying that makes much more sense.

So on the main page I need to create a structure that links the file with the fields that I am trying to reference. For example;

        fields:
          styles:
            label: Font Styles
            type: structure
            fields:
              fontfile:
                label: Font File
                type: files
                multiple: false
                query: page.files.template("fontloader")
              stylename:
                label: Style Name
                type: text
              instance:
                type: text

Now in my block I can select from that structure using?

query: page.styles.toStructure

Somewhat related but not vital.
If I wanted to auto populate this structure when a file is added I would use a hook correct?

Yes, that would have to be done via a file.create:after hook.

1 Like