Score:1

Restrict a Media field to allow selecting only a certain file extension in an existing Media Type

pk flag

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
leymannx avatar
ne flag
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?
pk flag
@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
leymannx avatar
ne flag
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)`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.