General Image Handling

Hello everyone,

I am still relatively new to kirby and have a question about images. I have already researched the fact that there is no direct “file manager”, but there are various workflows. However, none of them have solved my problem so far. I have the following requirements and hope that someone here can help me:

  • Images should be stored centrally (so that images that are used twice or three times do not have to be uploaded 3 times)

  • This also means that I should be able to access this “pool” of images from different pages

  • Uploading should still work via the subpages if possible

  • I came up with this idea to make it easier for my customer to delete images. I.e. the images should be able to be deleted via this central location

  • I have set various elements so that they cannot be saved without an image. So you shouldn’t be able to accidentally delete images that are being used. Gff you simply cannot display them. But only in the upload window.

An alternative would be to display on a subpage which images are in use on that subpage and can be deleted from there. Then you would have to upload images several times, but that would be ok for the size of the page. The only problem I have here is that the entire website is built with “blocks” and I cannot create sections.

Two options here:

  1. Save them directly under /content as files belonging to the site object
  2. Save them in a dedicated page

That’s no problem, you can query from site or this dedicated page as parent

No problem either by setting the parent as above.

That is generally a problem that is not so easy to prevent, but can be done, e.g via a hook if you know where your images are stored. It might make sense to somehow keep track of used images in that case.

Thanks for the answer! Do you have an example for any of your points? Im having a hard time to implement this. i understand the logic but i don’t know how to apply it to kirby.

Ad 1: Create central pool: E.g. Files | Kirby CMS, so a section in site.yml for the upload, similar for a dedicated page.

Access central pool: Usually in a files field via query, see docs: Files | Kirby CMS (or from the site) and set the upload parent: http://getkirby.com/com/reference/panel/fields/files#upload-options__destination-and-template. Works in the same way for uploads/select to a textarea, but since you are using blocks, that’s not relevant.

Hi @texnixe Your links refer to your local development environment :sunglasses:

Just testing if you follow the links :stuck_out_tongue:

I click on almost every link. There could be useful information hidden behind them :stuck_out_tongue_winking_eye:

So, I finally had the time to test your examples. It worked like a charm! Thank you very much :slight_smile:

Im trying to get into the hook stuff. I found the hook “file.delete:before”, configured it in my config.php and it is triggered. My problem is, that in the config.php, I have no acces to the $site variable or anything else, so i can check if the uuid of the image is used anywhere on the page. Do you have any idea of how to get the information I need?

You get access to the site object via the site() helper.

Okay, thanks for this answer!

Now I only have the problem, that I dont know, how to prevent the deletion of the file if the file exists. I have the logic for checking, if the file exists, but I dont know, how to prevent the actual deletion and returning a message back, which says something like “The file could not be deleted, because it is used” or something.

You need to throw an error

throw new Kirby\Exception\LogicException('The file cannot be deleted because it is used'):