Deleting image in panel, breaks website

A client deleted some images that have lead to the whole website breaking.

I’m checking if there is an image, and only show it, when it exists.

<?php if ($item->cover()->isNotEmpty()): ?>
<div class="frontcover"><img src="<?= $item->cover()->toFile()->url() ?>"></div>
<?php endif ?>

But if there was an image, and the client deletes it, the CMS does not update. There is no Save function requested unlike to other changes on the site. The fix is, to change something else on the page, save, change it back, save again.

What would be the best way of securing this is not happening in the future?
Upgrade to a new version, or implementing the image differently?

(I’m certain, the client removed the File and did not unselect the “Voorvertoning”, that way I get a Save request.)

The reccomended approach to showing an image in the template is

<?php if ($cover = $item->cover()->toFile()): ?>
<div class="frontcover"><img src="<?= $cover->url() ?>"></div>
<?php endif ?>

->toFile() returns null if the file does not exist. So you don’t enter the if statement’s body when the image no longer exists.

Can you post the Blueprint code?
There are two different files fields:

  • Files Section: File overview in the panel → Management of all files on the page.
    Here, the file is deleted directly without confirming the action with “save.”

  • Files Field: Input field → Selection of specific files to be used in the content.

Oh, thanks a lot. That was fast!
Fixed it and tested it. It dos no longer break the website :white_check_mark:

Yes, that’s the case as the screenshot shows. There is a Files overview on the right sidebar.
The client deleted the image in there instead of the input field on the left.
But with the code of @rasteiner nothing breaks any longer. :heart: