Why does file.*:after hook not fire?

Hi there

I’m trying to create a hook after a file has been uploaded/replaced in the files field.
But none of the file hooks fire. Am I misunderstanding how the file hooks work?
Because the page.update:after hook is working for me.

blueprint

fields:
  type: fields
  fields:
    excel:
      type: files
      multiple: false

in config.php

    'hooks' => [
        'file.create:after' => function (Kirby\Cms\File $file) {
            throw new Exception('file.create:after');
        },
        'file.update:after' => function (Kirby\Cms\File $newFile, Kirby\Cms\File $oldFile) {
            throw new Exception('file.update:after');
        },
        'file.replace:after' => function (Kirby\Cms\File $file, Kirby\Filesystem\File $upload) {
            throw new Exception('file.replace:after');
        },
        'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
            throw new Exception('page.update:after');
        }
    ],

All hooks apart from the file.replace:after look good and should work, the replace hook should have $oldFile and $newFile as params, not $file/$upload.

Thanks for the reply!

Even after fixing the parameters, the file hooks don’t trigger (or in my case don’t throw an exception) if I replace/upload a file to the files field on the page.

Hm, you cannot replace a file via the files field, but the hook should at least be triggered on upload.

You were right! I added the ‘file.create:after’ hook which then triggered when I uploaded the file!
So if I understand it correctly, the file hooks trigger when you actually interacting with the filesystem and not with the panel?

Well, the hooks trigger depending on the actions. file.replace actually means that you replace an already uploaded file with another file (see replace option in the file view). The file.replace hook doesn’t trigger if you select file a instead of file b in a files field.

1 Like