Is there a way to restrict a Media field using an existing Media Type (File) in a Content Type to permit only a certain file extension in that instance?

In particular, I need a subset of the permitted files types in the 'File' Media Type - just PDFs, without affecting the Media library or the 'File' media type.

I've tried creating a new Media type (in addition to Audio, File, Image, Video) and added a restricted File field that only supports PDFs, however because it's new there are no file entities to select; and I need to show a subset of the existing File library.

To demonstrate the desired behaviour:

Content type A

  • Media field > File, exposes all files supported by the 'File' Media type:
    • doc
    • docx
    • pdf etc

Content type B

  • Media field > File, exposes only one extension:
    • pdf
  • 1
    Don't you just need to create a new media type, for example "PDF", add a new file field to it and in the file field allow only pdf like in your screenshot?
    – leymannx
    Commented Jan 12, 2022 at 9:52
  • @leymannx That works for entirely new Media types, but revealed a snag; A new Media type begins with no entities, and all existing media (including the PDFs I need) are not available as they are saved as the 'File' Media type, and I need a culled list of existing File media - I've updated the OP
    – Timmah
    Commented Jan 12, 2022 at 22:16
  • Split them up. And then have different allowed media types per content type. Programmatically get all nodes having media of type file attached where the media contain a PDF, group them by media, migrate these media entities into new media of new type PDF and reattach to the grouped nodes you got at the beginning. Do the programmatic update in a batch process. Either form or hook_deploy_NAME/N(&$sandbox).
    – leymannx
    Commented Jan 13, 2022 at 0:26


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.