our marketing page runs on Kirby (www.appointmed.com) and we are in the process of updating a couple of things right now. I took over the project from another dev and I’m having issues adding a new image to a page.
There are already multiple images available and all I did was duplicate the respective code and change the variables.
Template/Snippet
$imageMain = $section->image($section->imageMain()); // getting the image
<img srcset=“<?= $imageMain->url(); ?> 1x” alt=“<?= $imageMain->altText()->html(); ?>” // showing the image
What I want to do is add another retina sized image ($imageMain2x) to the “img srcset” tag. Duplicating (and renaming all the fields/variable to $imageMain2x) results in an error:
The weird thing is that $imageMain works as expected, but as soon as I try to add the $imageMain2x in the srcset tag, I get the error mentioned above. (File is definitely available in the content folder)
For this second image, you also need your if statement (or ternary operator…). Guess that happens on a page where you have multiple sections with their teaser images, and if not all fields of all images are filled in, this error appears.
Whenever you deal with class methods like url() etc. always, always, always check, if you have an instance of that class before calling any of the class methods.
As I said above, as a general rule, never ever call a class method without making sure you have an instance of the class, in this case a file object, first.
The reason in this case is that if you save a filename in your content file and the file is later deleted, or the field is empty in one of the pages, then you don’t get a file object if you call $section->image($section->imageMain());
Without knowing the complete structure of your template, I can’t tell you exactly what’s happening. But this kind of error (“Call to a member function url() on null”, or "Call to a member function xxx() on boolean) is always thrown if the object is missing.