I regularly forget to add upload: image
to image specific upload file fields so that the image.yml file blueprint template will be used for those.
As soon as there has been an image already uploaded it then cannot be further modified by the fields of the image.yml even if I set the upload template option later.
Is there a way to define all general file type definitions in the default.yml file blueprint but only show the file type specific ones without setting a template?
Only via a little workaround that saves the file type to a hidden field and which you can then query using the when
option. But if you only want to allow certain uploads, for example, only upload images. But that only works for different fields or sections, if you want to limit uploads to a field, like only upload images (accept option), then you have to set the template.
In a case like this where I donβt limit the file type but only display images would it work with the mentioned hidden field and query logic in the default.yml?
cover:
label: Titelbild
type: files
max: 1
multiple: false
layout: cards
uploads: image
query: page.images
image:
cover: true
ratio: 16/9
For fields and sections it would work. You would store the file type in a hidden field via a file.upload.after
hook, then in your default.yml file blueprint use the when
option to set which field should be used for which file type.
I tried to assign a template automatically with a hook but couldnβt see anything happen after uploading an image.
What is missing?
'hooks' => [
'file.create:after' => function ($file) {
// do something before a page gets deleted
if($file->type() == 'image'):
$file->template('image');
endif;
}
],
If you want to assign a template at upload, you have to use $file->update()
, you cannot assign anything with the template()
method.
Thanks a lot, now it works as expected:
'hooks' => [
'file.create:after' => function ($file) {
// do something before a page gets deleted
if($file->type() == 'image' AND $file->template() == ''):
$newFile = $file->update([
'template' => 'image'
]);
endif;
}
],