Image template for files to only accept images doesn't work

I’m trying to create an image template for files to only accept images.

It’s an extension of the default file template.

files/image.yml

title: Image

extends: files/default

accept: image/*

My field:

sliderImages:
  label: Images
  template: image
  type: files

If I add accept: image/* to files/default.yml it works. But I’d like to create a separate template that is based on the default blueprint.

A files field doesn’t have a “direct” template option, the template is set under the upload option:

gallery:
  type: files
  uploads: 
    parent: site
    template: files-upload

Or are we talking about a files section?

1 Like

Thanks so much! That made the trick :+1:

Old post, but just spent an hour looking for this info.

It looks like the docs are false here:

Especially this part:

Restricting uploads to certain file types

If you want to restrict what types of files can be uploaded to the given destination, assign a file template using the uploads property. In your file template, set the accept option. See the docs about file blueprints.

I spent ages trying to make the accept option work in a files blueprint, and indeed it works… when you are in the edit view for a specific file and not in the edit view for a page which uses that file as a field value.

So it looks like you have to put the accept option twice:

  1. In the file blueprint (e.g. site/blueprints/files/image.yml).
  2. Under the fields.{field_name}.uploads key in the page blueprint.

(I verified this by looking at the hidden <input type="file" accept="…"> generated in the DOM.)

I don’t understand the issue:

Given you add a files field to a page blueprint:

    fields:
      gallery:
        type: files
        uploads:
          template: files-upload

And then in your files-upload.yml file blueprint

title: File uploads

accept:
  extension: png

You won’t be able to upload anything but .png files to this gallery files field? You don’t have to add the accept option anywhere else.

Hmm looks like you’re right.

I think my mistake was trying to use a template key for a type: files field, based on examples I was seeing in the Blueprint samples.

But double-checking now, it looks like those examples use type: files sections, not type: files fields, which is hard to tell because it’s implicit (the sections key is not used).

So I did not use a correct field config like this:

type: files
label: Cover image
max: 1
uploads:
  # this will use the "accept" config from
  # blueprints/files/cover-image.yml
  template: cover-image

Because I already had a template key one level higher, based on copy-pasted examples, which was ignored silently:

type: files
label: Cover image
max: 1
# this will NOT use the "accept" config from
# blueprints/images/cover-image.yml
template: cover-image
# so I had to redefine the same "accept" config here
uploads:
  accept:
    # …