I have an image inside my assets/images
folder which should be used as the ‘default page image’ within the Panel, if the user hasn’t yet uploaded another image for the page. In order to do that, I used a page model to create a default ‘main_img’ calculated property: if the page has an image, it will use that, otherwise it should use the new ‘Asset’ class - or the asset()
helper - to create a default image on-the-fly. The code I have for the model, and which doesn’t seem to work, is:
<?php
class TestPage extends Page {
public function main_img() {
if ($this->images()->findBy('template','preview_image')) {
return $this->images()->findBy('template','preview_image');
} else {
return new Asset("assets/images/default.png");
}
}
}
In the blueprint of the parent page, I have created a Pages section, which should list all my child ‘Test’ pages, using the “main_img” as a cover image - like this:
sections:
site_areas:
type: pages
headline: Test Pages
status: all
layout: cards
templates:
- test
image:
query: page.main_img
cover: true
back: white
If the user hasn’t uploaded an image, nothing is shown - i.e., it seems that the Asset class is not finding the image, or not creating the image object. I’ve checked that the image is has the right filename, and is in the right location, and I can access it directly with the browser. I also don’t get any errors or exceptions - I just don’t get the default image in the Panel, ever… Same thing happens if I use the asset()
helper.
What am I doing wrong?