Page image is not displayed in pages section when referencing a filtered out page

Hi,

I have a pages section in the site.yml where the pages all have a files field. This field has a query set to use another page’s files.

fields:
  page_icon:
    label: Page Icon
    type: files
    required: true
    multiple: false
    query: site.index.template('files').first().images
    uploads: false

In the pages section, the image property is set to use this field as cover image.

      test_pages:
        headline: Test Pages
        type: pages
        status: published
        layout: cards
        image:
          query: page.page_icon.toFile

If the section includes the page type referenced by the files field (templates array includes files) than the images are displayed but if the files page type is not in the templates array only the default icon is displayed for all the pages.

Is this a bug or a limitation of the query references? Is this documented somewhere I didn’t catch?

Thanks!

Minimal working example starting from the starterkit:

Summary
  1. Create two page templates, files.yml and test.yml

files.yml

title: Files
preset: files

test.yml

title: Test
fields:
  page_icon:
    label: Page Icon
    type: files
    required: true
    multiple: false
    query: site.index.template('files').first().images
    uploads: false
  1. Update site.yml at the bottom so both types can be created:
      pages:
        type: pages
        create:
          - default
          - files
          - test
        templates:
          - about
          - home
          - default
          - files
          - test
  1. Create a files type page, upload some images and publish the page.

  2. Create a test type page, select one image from the picker and publish the page.

  3. Update site.yml, create a new files section where both page types are supported:

 - width: 1/1
    sections:
      test_pages:
        headline: Test Pages
        type: pages
        status: published
        layout: cards
        image:
          query: page.page_icon.toFile
        templates:
          - test
          - files

Expected behavior: both pages are displayed, the test type is displayed with the selected image and the files one with the default icon.
Actual behavior: exactly this

  1. Update site.yml and remove the -files type from the template list leaving only test type
        templates:
          - test

Expected behavior: only the test type page is displayed with the selected image as cover.
Actual behavior: only the test type page is displayed, but with the default icon

I’d create a method that returns either the image from the field if it exists or a fallback image, e.g.

page.displayImage

Then create the page method displayImage(): Page methods | Kirby CMS

@texnixe thank you for the fast reply and the suggestion!

I tried the page method solution in a plugin:

  'pageMethods' => [
    'displayIcon' => function () {
        return $this->page_icon()->toFile();
    }
  ]

and updated the site.yml blueprint for the section:

        image:
          query: page.displayIcon

Unfortunately the bug stayed. If I leave the page type in the templates list which contains the files they are displayed but if I remove it (which is the intention) no image is displayed for the other page types either.

Because you don’t return a fallback as I suggested

 'pageMethods' => [
    'displayIcon' => function () {
        return $this->page_icon()->toFile() ?? $this->image();
    }
  ]

Good point, I updated the function. The problem unfortunately stayed. Just by adding / removing one line from the templates array (the blueprint type containing the images) in the files section, I can switch between displaying the default images or the ones selected in the field.

@texnixe I forked the starterkit and implemented a minimal working demonstration for both solutions, direct file access and page method.

The repository is here: GitHub - ersoma/starterkit-bughunt: Kirby's sample site – the easiest way to get started with Kirby

This is how the panel looks like:

Ok, now I understand the problem. That’s indeed really weird and seems to be a bug. I first thought it might be related to the page name files, but that doesn’t seem to be the case. Thanks for providing the Starterkit.

I did some debugging, tracing back the site/sections/(:any) API call all the way to the image function call in kirby/src/Panel/Model.php#L114 ($model->image() | Kirby CMS).

Here a protected imageSource function is called with the same parameter (page.page_icon.toFile) in both cases but returns a Kirby\Cms\File Object when the files blueprint type is part of the section and null if it’s not.

Interestingly, it starts to work if I replace status: published with status: listed or removing the status prop.

I created an issue on GitHub:

Nice catch, I didn’t notice that it depends on the status setting as well. Thanks for looking into it and opening the ticket!

This will be fixed in 3.7