Automate transfering files from files field to files section?

Hi there,
in an older project I was using the files field when I should have used the files section. Now that the project will be renewed I would like to transfer all the files from the files field including the fields like caption, copyright to the files section.

As my files section is called gallery I found out that I only had to add
Template: gallery

to the image.txt to make it show up in the files section. However I have hundreds of images stored this way and I was wondering if there was a way to somehow automate the process or if anybody has a better idea.

Thank you very much!

To clarify this: Your new files section has a template assigned to it that is not assigned to the images files that were uploaded through the files field?

One option would be to remove the template assigned to the files section so that all files show up, regardless of assigned template.

The other option is to automatically loop through all files (by type or whatever is your criterium), and update them using

kirby()->impersonate('kirby');
// filter files as needed, e.g. exclude the ones that already have
// the right template, by type etc.
foreach ( $site->index()->files() as $file ) {

  $file->update(
    [
      'template' => 'gallery',
    ]
  );
endforeach;

You can use this code in a template or controller (for one time usage), or in a route, or store it in a function for future use…

1 Like

Ah, removing the template makes much more sense and works totally fine! I somehow thought templates would be necessary for a files section. Thank you!

No, they are not, but useful for filtering if you have multiple files sections or want to restrict what can be uploaded.