Is it possible to get the file type in blueprint

Probably isn’t – figured I would ask anyway:

I’m setting up a “files” section on a Kirby site, where users can upload files with different mime types from the panel. They are rendered on the site using snippets:

foreach($page->files() as $file) {
    echo snippet('media/' . $file->type(), ['file' => $file]);

As mentioned I’m setting up the Panel to accept all kinds of files, which would ideally all have a different blueprint depending on the file type. There’s currently a master blueprint for all files uploaded. Can the panel blueprint automatically detect the file type? Or, if I set up different file blueprints and have the file section accept them, will it automatically detect which blueprint to use?

…ok nevermind, looks like I can just set up a file blueprint for each type and then list the different file blueprints(image.yml, pdf.yml etc) in accept on the “files page” blueprint

also found this now

Is that still a question or solved (because I don’t understand what you want to achieve)?

When I upload a file, I want it to have a certain blueprint depending on its file type (i.e an image has the option to add alt text, or a pdf will have the option of adding a description, something like that). I think this is solved, I will use hooks to assign a blueprint on file upload if there’s not another way to achieve this

There is no other way to achieve this at the moment.

Unless you use different upload sections per file type…

Ok – so if I use a hook like this it will work?

'file.upload:before' => function ($file) {
  if ($file->type() == 'image') {

I don’t really want to use different upload sections :confused:

Nope, you can’t do this in a before hook, you have to use an after hook.

tried this, but in the panel next to the image it still says

'file.create:after' => function ($file) {
  if ($file->type() == 'image') {
    $file->update(['template' => 'image']);

hm, that’s not working for me :confused: although I don’t understand why

edit: it worked when using file.create:after instead of file.upload:after!

Ah, of course! Totally overlooked that.

1 Like

i’ll still mark your previous post as the solution :~)

1 Like

I fixed it.

1 Like