Files vanish when I set template

I cant get the image blueprint to work. If I leave off the template it shows the files, clicking on them shows the details but says no template. If I set the template the files vanish from the section, but I can still access the directly.

sections:
  files:
    type: files
    template: image

If I set the filename inside blueprints/files to default.yml it works ok, but I have many file blueprints so this wont work in practice for me. If i set the template to be image nothing, zilch, cannot get them to show in the section.

Can’t see anything in the docs that gives answer to what is wrong here

So let me see if I understand your problem.
You have files already present in your folder.

If you create a files section without specifying a template the files are showing up correctly but if you set the template value to image where image is the name of your file blueprint the files are not showing up.

Is that correct?

Because if that’s correct, then that’s the intended behaviour.
When you upload a file, Kirby creates a .txt files for that image. If the template value is set, the image will receive that particular template value. At the same time, the template value will also filter out the files that don’t have a particular template from the files section.

Does that make sense?

To give you a bit more context check this example. I have two images, template-test.png and template-image.png and each have a txt file with the template name stored in it

With this code only one of the two images will show up…

sections:
    files:
        type     : files
        template : image

…And it’s the image with template: image set inside the txt file.

If I change the value inside my blueprint to template : test the other image will show up

And if you leave out the template : completely from the blueprint both image will be displayed in the list

In addition to @manuelmoreale explanations:

For the images to show up when template: image is set, you would have to (manually or programmatically) add that template name to your image metadata, which can be achieved with $file->update().

Ok, I get this now. This has risen from a K2 template conversion.

My old template had a files: section in the blueprint which had the definition. I am simply just trying to replicate the K2 panel layout. This more data conversion needed?

Yes, in K2 you defined the fields for the files directly in the page blueprint. Now you need a separate file blueprint.

The advantages of this approach: You can have more than one file blueprint per page, so if you need different types of fields for different types of files (e.g. images vs pdf files), you can do that now. And not only that: For each files section you can set what file size, dimensions, ratio, or mime type are allowed.

I understand the change, just how to get 300 articles with multiple files converted without a huge headache.

I moved the files section last night into the separate images.yml, and the images vanished, hence the forum post. So for every file I need to create a txt file indicating what template to use?

As I wrote above, you can do this programmatically using the $file->update() method. See the examples in the linked docs. In your case you would set the templateentry.

It would be good to add this to the K2->K3 migration script as it will affect everyone?

Right now I am having to write code to get any parity from a K2 upgrade, spent several hours last night trying to get the panel usable with my K2 content. I can see the improvements are great, but the upgrade is certainly proving more difficult that it should be at the moment.

Yes, I thought so last night while answering your questions but haven’t had time to add this to the issues. Will do now.

1 Like

Let me just test something that should actually make it easier for you.

Thanks, appreciated. Won’t be able to action anything until this evening.

If your files all use the same fields, you could use a default.yml files blueprint. This will be picked up by all images that haven’t set a specific blueprint.

I found that default worked last night, but sadly I have a number of different file structures. :frowning:

Was there an easy way worked out for this?

Ok, I have managed to update all my article files

This is the script in case anyone will find useful.

<?php

    include 'kirby/bootstrap.php';

    $kirby = kirby();

    foreach($kirby->site()->pages()->find('articles')->children() as $page):
        foreach($page->files() as $file):
            $file->update([
                'template' => 'image'
            ]);
        endforeach;
    endforeach;
1 Like

Thanks for the help @texnixe

@DanielRivers You are welcome, will try to post a little recipe later today, think this will come up more often.

1 Like

https://getkirby.com/docs/cookbook/migration/files