Kirby 3 - global image folder from panel

hi, i’m new to kirby 3 and no developer.

trying to solve a problem solved before in kirby 2.
like to have a global image folder.

in kirby 2 this did the trick.

/* Image Hooks ------------------------------------------------------------------------------------------------ */
// copy file on upload to assets folder
kirby()->hook('panel.file.upload', function($file) {
  copy( $file->dir() . '/' . $file->filename(), kirby()->roots()->assets() . '/images/' . $file->filename() );
});
// delete file on delete from assets folder and upload folder
kirby()->hook('panel.file.delete', function($file) {
  unlink( kirby()->roots()->assets() . '/images/' . $file->filename() );
  unlink( $file->dir() . '/' . $file->filename() );
});
// replace file on replace from assets folder and upload folder
kirby()->hook('panel.file.replace', function($file) {
  copy( $file->dir() . '/' . $file->filename(), kirby()->roots()->assets() . '/images/' . $file->filename() );
});

how can this be done in kirby 3 ??

help appreciated

thank you

All you need to do is put a files section somewhere, perhaps in site.yml or even make a dedicated page. This will allow you to upload files. If you wish, you can then use subpages to make albums up, to group certain kinds of images together.

Then you can use the new query language on a files field in your other pages to pull those images in, regardless of where they are. Theres an example in reference for doing this on the files field.

What is your use case for copying all files to the assets folder? In Kirby 3, all files are moved to the media folder (i.e. outside the content folder) by default, anyway, so copying to the assets folder additionally doesn’t seem to make that much sense.

If you still want to move all images to the assets folder, you can of course do that using Kirby’s hooks as before. The only thing that has changed is the name of the hooks and how you register them.

thank you for helping me @jimbobrjames @texnixe

the main reason for having all images in one folder is maintenance.
the only files in the media folder are the thumbnails and the main images are in the content folders.
so if i upload images inside the panel, they are moved to the media folder. but these images are thumbnails.

i have tried to use the new hook like this

return [
  'hooks' => [
    'file.create:before' => function ($file, $upload) {
      F::write( kirby()->roots()->index() .'/logfile.txt', 'write something' );
    }
  ]
];

nothing happens. no file created.
i’m using kirby 3.1.1

any ideas?

thank you

A file.create:after hook would make more sense, I think.

If you wrap your code blocks between three backticks before and after the block, your code instantly becomes more readable.

good point “file.create:after”
but makes no difference, file not written anyway.
F::write( kirby()->roots()->index() .’/logfile.txt’, ‘write something’ );

Works for me. Where are you using this code? In the config? Does the config file contain anything else?

yes in the config.
site/config/config.php

return [
    'debug' => true
];

return [
  'hooks' => [
    'file.create:after' => function ($file, $upload) {
      F::write( kirby()->roots()->index() .'/logfile.txt', 'write something' );   }
  ]
];

thats all

Ah, that’s what I thought. Could can’t have two return statements, when a return statement is encountered the control is given back to the calling script, so anything following that return statement is ignored. so you have to put it all into the same return array.

Again, please mark up your code in backticks please, like this:

removed debug statement
now the write statement is working and the logfile.txt is created. good.

return [
  'hooks' => [
    'file.create:after' => function ($file) {
      F::write( kirby()->roots()->index() .'/logfile.txt', 'write something' );
      F::copy( $file->dir() . '/' . $file->filename(), kirby()->roots()->index() . '/images/' . $file->filename() );
    }
  ]
];

but the copy statement is not working. bad.
maybe i have to replace $file->dir() with $file->root() because can’t find dir() in reference.

i’ve used the backticks ``` now. better?

Yep, there is no dir() method anymore.

:+1: Great! Thank you!

f::copy( $file->root().'/'.$file->filename(),kirby()->roots()->index().'/images/'.$file->filename() );

well, seems F::copy is not working at all.

Should be kirby()->root('index')

No, both are possible.

Your original code with the PHP copy function should still work, apart from the dir method.

thank you
but
does not work

'file.create:after' => function ($file, $upload) {
    copy( $file->root(), kirby()->roots()->assets() . '/images/' . $file->filename() );
}

This works for me.

hrmm… once again i’ve failed at searching the docs :slight_smile:

On a side note, you don’t have to remove the debug option, just put it all together into one return array:

return [
    'debug' => true,
    'hooks' => [
      'file.create:after' => function ($file, $upload) {
        // code 
      }
    ],
    'some_other_option' => 'some_value'
];

thank you @texnixe

return [
  'hooks' => [
    'file.create:after' => function ($file, $upload) {
      copy( $file->root(), kirby()->roots()->assets() . '/images/' . $file->filename() );
    }
  ]
];

you did it
you’re my angel

thank you once more

2 Likes

:angel: