Set template with file.create:after not working

hi,

Each portfolio project on my website have a media gallery where I would like to upload both videos and images.

To give each file type it’s own template I am using the file.create:after hook to assign the appropriate template based on the file type.

The hook does not seem to work at all, and I am now stuck with no idea what would be wrong.

Any help or pointers in the right direction is greatly appreciated!!

site/config.php:

'hooks' => [
        'file.create:after' => function ($file) {
            foreach (kirby()->languages() as $lang) {
                if ($file->type() === 'image') {
                    $file->update([
                        'template' => 'image'
                    ], $lang->code() );    
                } elseif ($file->type() === 'video') {
                    $file->update([
                        'template' => 'video'
                    ], $lang->code() );
                }
            }
        }
    ]

Project.yml blueprint ( I am uploading the mixed media through the mediaGallery )

# 
# Single project page blueprint. 
title: Project
status:
  draft: Draft
  unlisted: Unlisted
  listed: Published
columns:
  media:
  - width: 1/2
    sections:
      mediaGallery:
        type: files
        layout: cards
        label: 
          en: Photos
          dk: Fotos
        size: tiny

  information:
  - width: 1/2
    fields:
      tags:
        type: tags
        label: Categories
        min: 1
        accept: options
        options:
          - food
          - design
          - interior
          - cosmetics
          - film
          - portrait
      description:
        type: textarea
        size: small
      credits:
        label: Credit list
        type: structure
        fields:
          name:
            label: name
            type: text
          role:
            label: role
            type: select
            options:
              makeup:  Makeup
              hair: Hair
              styling: Styling
              talent: Talent
          link:
            label: link
            type: url

I also tested this with the starterkit.
It works with v3.7.4 but it doesn’t work with v3.8.2.

PS: Do file template changes have to be made differently now or is this a bug?

File template updates still work as expected, had to do this several times last week (only tested in non-multilanguage though).

@Cris What code did you use to update? Also in a hook or just a script?

@texnixe
I tried a file:create:after hook in the config file based on this quicktip with two linstalled languages.
Didn’t test it with a “normal” script.

// config.php

return [
  'debug' => true,
  'whoops' => true,
  'languages' => true,
  
  'hooks' => [
    'file.create:after' => function ($file) {
      $kirby = kirby();
      $tpl = null;
      $type = $file->type();
      
      switch ($type) {
        case 'image':
          $tpl = 'super-awesome-image';
          break;
        case 'video':
          $tpl = 'super-awesome-video';
          break;
      }
      
      $data = [
        'something_else' => 'gets updated!',
        'template' => $tpl // doesn't get updated
      ];
      
      try {
        // if we have a multi-language site, we loop through all languages
        if ($kirby->multilang() === true) {
          $languages = $kirby->languages();
          foreach ($languages as $language) {
            $newFile = $file->update($data, $language->code());
          }
        } else {
          $newFile = $file->update($data);
        }
      } catch (Exception $e) {
        $messages[] = $file->filename() . ' could not be updated (' . $e->getMessage() . ')';
      }
    }
  ],
];
# note.yml
# just for testing 🙂
columns:
  - width: 1
    sections:
      all_files:
        type: files
        info: "tpl: {{file.template}} | sthelse: {{ file.something_else }}"