Add images to pages by file name?

Hi there,

I want to set up a site with a lot of pages for different products. I wouldn’t like to upload the product images manually in the panel one by one.

Does anybody know a way to e.g. upload all images named by the product they display into one folder and then let the product pages grab their images on their own?

Any help is very much appreciated!

Sigi

If the product pages don’t exist yet, you could use a page.create:after hook that fetches the relevant image(s) for the product from such a pool folder.

@texnixe Thank you, I’ll try that.

Okay, so I tried to do the following:
The pages are created with Pages::factory as virtual pages. Then I added the hook to the config.php:

'hooks' => [
    'page.create:after' => function ($page) {
      if ($page->hasTemplate('variable')) {
        $this->impersonate('kirby');
        $files = scandir($page->kirby()->root() . '/productimages/' . $page->slug());
        foreach($files as $key => $val ) {
          $page->createFile(
            [
              'source'   => $page->kirby()->root() . '/productimages/' . $page->slug() . '/' . $val,
              'filename' => $val,
              'template' => 'cover'
            ]
          );
        }
      }
    }
  ]

When I try to add a page in the backend (which triggers the creation of virtual subpages), I get the following error:
The file exists and cannot be overwritten

I know that this is very little information, but maybe you see some problems in my code or in the things I’d like to accomplish generally.

Any held that points me in the right direction is very much appreciated.

Oh, I actually found a probably easier solution here: https://getkirby.com/docs/guide/virtual-pages/content-from-api

Thank you anyways @texnixe!