".or" in Kirby Query Language

Does the “.or” syntax work with toFile queries in a pages section? I see it in the docs but not for this use case: some of my projects have a thumbnail set but others have a first block with an image.

This shows preview images…

...
    listed:
        headline: Projects
        type: pages
        status: listed
        limit: 500
        image: page.thumbnailimage.toFile

and so does this…

...
    listed:
        headline: Projects
        type: pages
        status: listed
        limit: 500
        image: page.galleryblocks.toBlocks.first.content.image.toFile

but this doesn’t show any preview image…

...
    listed:
        headline: Projects
        type: pages
        status: listed
        limit: 500
        image: page.thumbnailimage.toFile.or(page.galleryblocks.toBlocks.first.content.image.toFile)

As a field method, or can only be called on a field object. When you call toFile() on a field object, however, either a file object or null is returned, so calling or on the result of toFile() will throw an error.

So you can call it on something like $page->title()->or('myfallbackvalue') or on a field method that returns a field object (there are only a few that do that).

I would use a custom page method or model method.

Check out the cover method in the album or note page models in the Starterkit for an example model method: starterkit/album.php at main · getkirby/starterkit · GitHub

1 Like

I have a feeling there is a silent error going on here. Do you see anything error popups or anything in the console?

You probably need a custom method here.

Edit: @moonwalk beat me to it :wink:

1 Like