I’m updating my site from Kirby 2 to 3, and I’ve run into a problem. I have several image fields for main page image, page background and so on. They all work in that I can choose a page image and the filename gets added to the plaintext file for the page.
Thing is, when I edit a page Kirby adds a '- ’ (a dash and a space) before the filename. There didn’t used to be, and it’s breaking my templates. I’ve tried ‘$page->bgimage()’ and ‘$page->bgimage()->filename()’ (bgimage is one of the fieldnames) and nothing gets returned. If I remove the hyphen manually, it all works again.
Before:
img_0635.jpg
After:
- img_0635.jpg
What am I missing? Is that hyphen supposed to be there, and why? Also, why does it break the above functions?
Edit: in my blueprint I have this kind of set up for each image:
K3 store files in yaml format, after all, the field can store one or multiple files. You can still get the image object using the toFile() method like in K2:
Thanks for replying! I was using that before, and kept getting PHP errors. Right now if I try that on my main template it returns the filename with the hyphen “- img_0635.jpg” and on my header snippet where I put in a background image I get a fatal-looking PHP error. Screenshot from debug:
Don’t know what’s happening on your side, but I just tested this in a project I’m converting to Kirby 3 and it works as expected.
Are you calling this code in a loop where not all images have the field filled in?
I don’t get enough context from your code snippet, but the way you coded that is not recommended anyway, because you should never ever call a class method (like url() in this case), without making sure you got an object to call it on. So either use an if statement like above or a ternary operator:
Thanks! I’ll give that a go, I realise my mistake now you’ve pointed it out - I’m checking for the existence of the wrong field! I’ll check it again tomorrow.
The other page was still returning the hyphen as part of the file name, I’ll post the chunk of code in the morning. Thanks for your help.