How to show thumb of certain file template in panel?

There is page template “product”, which contains files sections with 3 different file templates (product photos, drawings photos, detailes photos). I need to show first image of “product photos” as thumb in panel. Using this model:

<?php

class CategoryPage extends Page
{
    public function cover()
    {
return $this->content()->get('cover')->toFile() ?? $this->files()->filterBy('template','productphotos')->sortBy('sort', 'asc')->first();
    }
}

It works well for templates, but in panel shows first image of all images. How to make filter by file tempalte work in panel?

Do you mean as preview in a pages section?

yes

Could you please post the code you tried in the blueprint?

          products:
            type: pages
            create:
             - product
            templates:
             - product
            layout: cards
            size: tiny
            image:
              back: white
            info: false

Also tried image: page.cover, as I understand from docs Pages section | Kirby CMS, it should help, but doesn’t

Your page model refers to a CategoryPage type, however, in your pages section, you are using pages with the product template. So you cover method defined in the CategoryPage model won’t apply.

Sorry for confusion. But I also have same model for ProductPage, so it’s not the case:

<?php

class ProductPage extends Page
{
    public function cover()
    {
        return $this->content()->get('cover')->toFile() ?? $this->files()->filterBy('template','images')->sortBy('sort', 'asc')->first();
    }
}

Maybe the reason is that there are no any file fields, only file sections, but I guess it should be still possible to filter by file template.

Good, found solution. This works:

        image: page.images.findBy("template", "productphotos")

Or:

        image: page.cover

Only first I had mistake, this was wrong:

image: page.cover
  back: white

Should be:

image:
  query: page.cover
  back: white
2 Likes