Files field only images


#1

Ahoi,

https://getkirby.com/docs/reference/panel/fields/files

how to limit the selection to pictures?

  gallery:
      label: Gallery
      type: structure
      style: table
      fields:
        gallery_img:
          label: Image
          type: files
          multiple: false
        gallery_img_description:
          label: Image description
          type: text

#2

With a custom query (default is page.files):

gallery_img:
  type: files
  query: page.images

#3

okay thank you.
where can i find in the doc’s information what terms i can use
page.image
page.video
page.pdf ?


#4

You can use the same methods that are available as page methods:

page.images
page.files
page.documents
page.videos
page.audio
page.code

#5

thank you @texnixe :pray:


#6

Note that a files field query gives you pretty much control over what files you want to see, for example:

    fields:
      files:
        type: files
        query: site.find('photography').children.images.filterBy('template','sometemplate')

#7

Thank you @texnixe for your detailed explanation on how the queries work! I’m trying to realize two separate sidebar groups for “Images” and “Documents”, but the queries don’t seem to work for me right now…
I’m extending the page preset by adding

sidebar:
  images:
    label: Images
    type: files
    query: page.images
  documents:
    label: Documents
    type: files
    query: page.files.filterBy('type', 'document')

I’m not sure if filterBy('type', 'document') is even valid, but also the “Image” group is showing uploaded JPGs as well as PDFs…

What am I doing wrong? Could it have anything to do with the options set in my files blueprint?
Thanks for your answer!


#8

You probably need set the mime type on the section. This will restrict that section to only accept files of that mime type (and display only files of that type). But the query should work too, what you did looks ok to me.

for example, my docs.yml that gets used by the files section looks like this:

title: Docs

accept: application/*

And my images one looks like this:

title: Image

accept: image/*

To make it work you need to set the you files section to use that template:

docfiles:
  type: files
  template: docs
  headline: Documents

#9

Oh yeah, the section template is a good idea, yet I don’t seem to grasp how to apply it to my case (had two beers just now, abstract thinking is getting harder…) especially since you also think the query should be working! Plus I don’t want to add too many templates, this specific section is not going to be reused again in my project.

On an unrelated note, I don’t know why my confused brain thought label was the proper way to give the field a headline. Thanks for clearing this up :smile:


#10

Don’t panic, i’ve had a couple of beers too :slight_smile: It just needs to go in the file meta you want to use for those types of files. Be aware that application/* does snag a couple of file types that you may or may not want someone to upload (like binary files). To get around that you would have to list out specifically all the mime types you do want to except. I was just lazy and used the wildcard.


#11

I’m always baffled by how people can drink and still be able to write good code. I recently accidentally removed a whole page before going to bed and woke up to a bunch of confused mails from my client… Luckily it wasn’t a production server!

Anyhow – right now, none of your suggestions work for me (after adding the section template, no files are showing at all and I can still upload files of all mime types. I guess the problem lies somewhere else because in theory it all makes sense… I’ll go to sleep & find the solution tomorrow :slight_smile:

Thanks for your help!


#12

It will do that, yes. Once you have added the mime type settings, it stamps the uploaded files in the content file. Its empty becuase you havent upload anything into that field yet. You have files that match already on the server, but they didnt pass through that field, so they wont show up. Confused me at first, and its a little odd, but there it is.

Not sure why it isnt respecting the mime type when you upload though, it works for me.


#13

First of all, there is no query option for files sections, you can only set a parent and a template.

In your template, you can then use the accept option to limit mime types.


#14

Thank goodness for someone who hasnt had beer :slight_smile:


#15

Thanks for your answer! I’m doing this now:

sidebar:
  images:
    headline: Images
    type: files
    template: images

and in my sections/images.yml:

title: Images

accept: images/*

yet:

39

:confused:

edit:
realized about the images -> image typo. Doesn’t change anything though…


#16

Thats the wrong place. Should be in files/images.yml


#17

OHH oops. :flushed: works now. Thanks a lot @jimbobrjames & @texnixe!