Trying to rename file on upload with a hook

Hello everyone,

After stupidly losing time with A"I", I come to you, dear humans.

I’m trying to automatically rename the cover image for my articles to “cover-image.jpg”. To do this I tried the following hooks which both don’t work :

Hook 1, simple :

'file.create:after' => function (Kirby\Cms\File $file) {
  if ($file->template() === 'cover-image') {
    $parent = $file->parent();
    $originalName = $file->filename(); // e.g. "image-truc.jpg"

Hook 2, more complicated :

'file.create:after' => function (Kirby\Cms\File $file) {
  if ($file->template() === 'cover-image') {
    $parent = $file->parent();
    $originalName = $file->filename(); // e.g. "image-truc.jpg"

    // check if the picture already exists
    $existing = $parent->file('cover-image.jpg');
    if ($existing && $existing->uuid() !== $file->uuid()) {
      $existing->delete();
    }

    // rename
    kirby()->impersonate('kirby', function () use ($file) {
      $file->changeName('cover-image');
    });

    // remove the old .txt orphan in case that was the problem...
    $orphan = $parent->root() . '/' . $originalName . '.txt';
    if (file_exists($orphan)) {
      unlink($orphan);
    }
  }
},

What happens is that the card section in the panel becomes red right after I tried to upload the picture, and the article’s folder content becomes :

article.txt
cover-image.jpg
cover-image.jpg.txt
my-original-pic.jpg.txt

I’ve read all the other posts about this topic and none solved my problem. I’d like to avoid the problem entirely but I need to rename the file to distinguish it from other pictures using something else than Kirby’s internal file references system in case I want to migrate later. (If the cover-image field saved in the content’s article.txt was the filename and not a uuid that would solve the problem but it seems that we can’t do that.)

For info here is the relevant section in my article.yml file :

sections:
  cover:
    label: Image d'en-tête de l'article
    type: files
    min: 1
    max: 1
    layout: cards
    image:
      ratio: 3/2
      cover: true
    template: cover-image

and here is my cover-image.yml file :

title: Cover Image

accept:
  mime: image/jpeg, image/png, image/webp

create:
  format: jpg
  width: 1200
  height: 800
  crop: true
  strip: true

focus: false

options:
  changeName: true
  changeTemplate: false
  replace: false
  delete: true



Thank you for this great software !
Timo

I can’t re-edit the first post which has a mistake because I messed something up with the code block, but here is the first hook which didn’t work and I’d like to know why :

'file.create:after' => function (Kirby\Cms\File $file) {
  if ($file->template() === 'cover-image') {
    $file->changeName('cover-image');
  }
},

OK the problem was solved by returning instead of just calling the method, so the correct hook is

'file.create:after' => function (Kirby\Cms\File $file) {
  if ($file->template() === 'cover-image') {
    return $file->changeName('cover-image');
  }
},

I don’t understand the hooks dynamics very well but that does work. Thanks to this :