"Kind of" uploading workflow (templates, multiple files sections)

Hi there,

i’d like to solve to following thing:

  • on a blog page blueprint there is a files section called “inbox” using the inbox template which accepts certain file types
  • once a file has been uploaded a validation step hooks in using janitor jobs
  • certain files which are in the right format shall be “forwarded” to the media pool which means i’d like to change the files’ template to medipool
  • these files shall be listed in a second files section with mediapool template filter
  • other files will be converted into a more common format and then being forwarded to the mediapool

I am wondering how i could solve:

a) changing the template of a given file afterwards (through file.create:after hook)
b) prevent a files section from uploading files to it as all files shall be uploaded only to the “inbox” where the “validation” magic is triggered

Any idea? Appreciate all hints and tips,

Best, Thomas E.-E.

/cc @bnomei

executing

        try {
          $file->update(
              [
                  'template' => 'mediapool',
              ]
          );
        } catch (Exception $e) {
            return $e->getMessage();
        }

in a hook doesn’t seem to work by the way …

Updating the file like that should work. Could you post the complete code?

Here is the pice of my plugin code:

    'hooks' => [
        'file.create:after' => function ($file) {
            if ($file->type() === 'audio') {
                changeTemplate($file);
            } else if ($file->type() === 'video') {
                changeTemplate($file);
            }
        }, [...]

and

function changeTemplate($file)
{
    try {
        $file->update(
            [
                'template' => 'inbox',
            ]
        );
    } catch (Exception $e) {
        return $e->getMessage();
    }
}

which unfortunately doesn’t work :frowning:

(and yes, changeTemplate is being called)

Not sure about that but I guess you have to actually “save” the file after updating it.

function changeTemplate($file)
{
    try {
        $newFile = $file->update([
             'template' => 'inbox',
        ]);

        $newFile->save();

    } catch (Exception $e) {
        return $e->getMessage();
    }
}```

no, save() is implictly called by ModelWithContent::L779 … i drilled it down to

File::L202 where the template explicitly got overridden by $this->template and there is no setter or changeTemplate method to change the given template afterwards. Which leads me back to my original question ;-).

Any idea on how to change a files’ template afterwards? Probably i have to somehow create a new file with the intended template and deepCopy the original content into this new file with the new template?

/cc @texnixe

$file->update() is the right way and should work.

    'hooks' => [
        'file.create:after' => function ($file) {
            try {
                $file->update(
                    [
                        'template' => 'blabla',
                    ]
                );
              } catch (Exception $e) {
                  throw new Exception( $e->getMessage() );
              }
        },
    ],

no, it doesn’t unfortunately as template is overriden bei $this->template() in File:L202 (see kirby/File.php at 3.5.7.1 · getkirby/kirby · GitHub) … any other idea?

Hm, I can’t reproduce your issue, works fine in my installation.

hmm … i saw code branches in case of multiglange versus single language installation. Could this be difference? I am on kirby 3.5.7.1 . I will have a look through the code and search for concurrent triggers in other plugins. Also my trigger is located within a plugin and not config.php.

But good to know, that a trigger should work in general :slight_smile: Thanks!

In a multilang installation, you have to pass the language code of the language you want to update as second argument. If you don’t, the default language is used.

ok, i figured out the culprit (as it seems).

In my setup there are to file.create:after hooks configured. One in config.php which updates the file and adds creator (user) and createdAt (date). And another hook in a dataformat conversion plugin which converts an m4a into an mp3 file.

After removing the hook in config.php the template is updated properly. So it seems to config.php hook which is called after the plugin hook overrrides the changes again.

Is there a way to break to hook-chain programmatically? Is there a rule that plugin-hooks are called before config.php hooks?