Files field: query images in a block that works both on page and in site

Hi, I have a query for a files field, that I use in all of my blocks

query: site.images.add(page.images)

So I can use site images on pages too.
But, unfortunately, that query drops an error if I use the same blocks in the site blueprint

Your query must return a set of files

Because of that pages.images.

Any idea how to make that work?
Tried many things, even site.images.add(page.images ?? null) but that doesn’t work too.

Is there maybe a way to check if it is a site or a page with a query in a blueprint?
Something like:

query: 'site.blueprint ? site.images : page.images.add(site.images)'

Use a custom site method instead

Hi Sonja, thank you.

But how to call page images in a Site Method?

Hmm, I made a break on this but got back to it.

I am trying the custom page method at the moment. Is it possible to get an error of any kind for page.images query in the site blueprint?

'pageMethods' => [
        'getImages' => function () {
            if($this->files()->isEmpty()) {
              return site()->images();
            } else {
              return $this->images()->add(site()->images());
            }
        }
      ],

This is just the latest try to get any kind of error or exception for page.images query in the site blueprint. Whatever I try I get Your query must return a set of files.

    'siteMethods' => [
        'getImages' => function () {
            return $this->images()->add(page()?->images());
        }
    ],

Thank you, Sonja :slightly_smiling_face:

Tried something similar, and that’s definitely the closest thing.
But page()?->images() in site blueprint actually gives Home page images. On any page it will find Home page images.

Hm, then exclude the home page? Didn’t have any images in home:

            return $this->images()->add(page()->isHomePage() === false ? page()?->images() : null);

Now it only lists Site images, in Site, and in any Page.

It is like page() doesn’t detect where it is, or to say it differently, page() returns home page.

Then a page method

    'pageMethods' => [
        'getImages' => function () {
            return $this instanceof Site ? site()->images() : $this->images()->add($this->site()->images()) ;
        }
    ],

And in site.yml

query: site.page.getImages

Hm, interesting, but the results are the same as

 'siteMethods' => [
        'getImages' => function () {
            return $this->images()->add(page()?->images());
        }
    ],

and

query: site.getImages

It lists Site and Home images

Not for me, but I’m testing in a page blueprint. Are you in custom blocks blueprints? Maybe that’s the problem

Ok, I will recheck everything.
It is not a block blueprint, I have a test field directly in the page and site blueprint.

Hm, rechecked everything, it is good.

But it still lists Home and Site images with

query: site.page.getImages

When I change to

query: page.getImages

It works ok in page, but drops an error Your query must return a set of files in Site.

I will test tomorrow in the Starterkit…

Hm, just tested in Starterkit.

I added an image select field in Site, Home, and Note blueprint.
And added images additional images to Site and Home.

Wherever I try it lists Site + Home images.

There is only one thing left to check, which could be different compared to your setup. All my test installs are in a subfolder.

Just tested with the install in the root, it does the same, lists the Home and Site images wherever I test.
Maybe I did

My single file plugin index.php

<?php

Kirby::plugin('zero/get-images', [
    'pageMethods' => [
        'getImages' => function () {
            return $this instanceof Site ? site()->images() : $this->images()->add($this->site()->images()) ;
        }
    ],
]);

Cover field in Starterkit

type: files
multiple: false
query: site.page.getImages
uploads:
  template: image

Maybe it would make more sense to extend the field in site to use a different query instead of just making things complicated.

Well, that turns out to be much more complicated, because I would need to duplicate every block I want to use in Site that has the image select field.

And many blocks have some image options. It is a maintenance mess to have different blocks for pages, and different blocks for the site.

I am using a full-featured Layout Builder with blocks for footer creation, which is the same Layout Builder with blocks I use for pages.

It is not a super big issue, but it is a very irritating issue, that I just can’t solve.

I would actually be happy with any query that can search for images but can be used on the Site and Page.

Oh dear God, I just started writing Github issue and checked the default Image block query, and it has

query: model.images

Which works both on Page and Site.

Oh my, this issue has been irritating me for months, but the solution was always there. I think I gotta change business, buy some sheep and live on top of the mountain.

Could you please add query: model.images somewhere in the documentation as a viable option, it doesn’t actually exist there, someone could be going through the same pains as I did, but doesn’t see the solution.

1 Like