Page Builder isNotEmpty error after image deleted

Hi all,
I’m using the page builder from @timoetting, following the steps to get images in the backend and front from a snippet. All worked properly until i erased an image to replace it with another.
I thought the isNotEmpty would prevent an error if the picture field was empty.

I’m a bit clueless on what I’m doing wrong, I’m probably mistaking somewhere.

Thx in advance for the help.
Théo G.

<?php if ($data->picture()->isNotEmpty()): ?>
 <img src="<?= $page->image( $data->picture() )->url() ?>"> 
<?php endif ?>

Whoops\Exception\ErrorException thrown with message "Call to a member function url() on null"

I hope this bit of code is anough.

Is the field really empty? If you remove an image, the field value is not automatically deleted. In any case, it is recommended to check if an (image) object exists before calling a method on it:

<?php if ($data->picture()->isNotEmpty()): 
  $image = $page->image( $data->picture() );
    <img src="<?= $image->url(); ?>"> 
  <?php endif ?>
<?php endif ?>

Try this … (untested)

<?php if ($data->picture()->isNotEmpty() && $page->image( $data->picture() )): ?>
 <img src="<?= $page->image( $data->picture() )->url() ?>"> 
<?php endif ?>

The Problem is, that $page->image($data->picture()) doesn’t return an image object, because the image was deleted. And so the url() method can’t be used afterwards.

Or even shorter

<?php if ($data->picture()->isNotEmpty() && $image = $page->image( $data->picture() )): ?>
 <img src="<?= $image->url() ?>"> 
<?php endif ?>
1 Like

Super great ! Thanks a lot @texnixe verifiying if there was an image corrected my issue and @flokosiol for the extra information, now i understand a bit more how this thing works :slight_smile:.
Kirby community sure is a quick one.

Wish you both a good day.

Théo G.