I would like to create image blocks for each file upload to an image section. I tried to follow this recipe. The following hook works for single images. When I upload multiple images at once, it only adds one block to the blocks field.
'hooks' => [
'file.create:after' => function ($file) {
$kirby = $this;
$page = $file->parent();
$blocks = $page->projectContent()->toBlocks();
$imageBlock = new Kirby\Cms\Block([
'content' => [
'location' => 'kirby',
'image' => [$file->filename()],
],
'type' => 'image',
]);
$blocks = $blocks->add(new Kirby\Cms\Blocks([$imageBlock]));
$kirby->impersonate('kirby');
$page->update([
'projectContent' => json_encode($blocks->toArray()),
]);
}
]
As i understand the hook file.create.after is called for each file?
Or does this get handled differently when uploading multiple files at once?
At the moment I don’t see another approach doing something like this, other than a hook. But maybe there is another way to do this?