Files field missing template

Hello,

I need to assign a predefined template to the file meta after uploading a file via type:files field.
The field is embedded via extends:.

The template (e.G. hero) is not added to its meta data file. These are my 2 blueprints.

(For test purpose, I added parent:site which is running as expected and uploads the file to the site root.)

block.yml:

name: My block
icon: smile
tabs:
  media:
    label: My media
    fields:
      image:
        extends: mediafiles/hero

mediafiles/hero.yml:

label: My label
type: files
uploads:
    parent: site
    template: hero
query: site.images
layout: cards
size: medium
multiple: false
empty: Placeholder text

Thanks for any help in advance.
Chris

Tested your example and the hero template is correctly assigned to the uploaded file, so I cannot reproduce the issue.

Which Kirby version are you using, the latest 3.7.5?
Which other plugins are installed?

The project uses the currently latest Kirby version 3.7.5
But the code above is part of an own plugin. Probably the template name gets lost somewhere.
Any ideas of how to debug?

The file blueprint doesn’t have to exist in order to be assigned to a file’s meta data. But of course you need it if you want to fill the fields defined in that file blueprint.

Best way to debug would be via xDebug. Put a breakpoint in /kirby/api/config/routes/files.php, line #43, then check the request data as a first step.

I found the problem. Not yet the solution, but at least the problem :slight_smile:
This is my file blueprint:

sections:
  content:
    type: fields
    fields:
      template:
        label: Template
        type: select
        options:
          - hero
          - gallery
          - etc...

Fieldname may not be “template”. If I change it to “test” it is working.

In my use case, I need the possibilty to change the files template at a later time via blueprint. This is why I added the select field above. Probably I can solve it with a hook. :slight_smile:

I could solve it via hook. Attached the updated files in case someone has the same problem.

the new file blueprint:

sections:
  content:
    type: fields
    fields:
      newtemplate: // <- this name may not be "template"
        label: Template
        type: select
        options:
          - hero
          - gallery
          - etc...

config.php:

'hooks' => [
  'file.update:after' => function($newFile, $oldFile) {
    $newFile->update([
      'template' => $newFile->content()->newtemplate()
    ]);
  }
]

Still don’t really understand how you tried to assign the user-selected template (using query language?) when it wasn’t even defined at upload.

I have a couple of different custom blocks (e.G. hero, feature, testimonial, etc). The used image(s) get the block name as template. So i know which image is used by which block per page. By changing the image template, its possible to use it with a different block than it was initial uploaded.

query: site.images

is for the filepicker, not for the upload, if I understand it right