Score:0

How to set media library widget so that allows only certain dimension images for each field?

in flag

I'm trying to find a way to set some image dimension limit for the media library widget. That will allow me to restrict on which image sizes can be selected based on the fields they are using them.

I know that you can set a global min and max image size but the problem with this is that if you want to upload 300x300px image sizes for a certain field only and all the other images on the site are larger than 800px then you are forced to use 800px images for all fields as that would be the min size you could upload.

Is there a hook that I can implement in a module so that I can extend this functionality or a module that can give me this more granule control?

Score:0
aq flag

You could add validation against each field with something like this https://www.drupal.org/docs/drupal-apis/entity-api/entity-validation-api/defining-constraints-validations-on-entities-andor-fields

in your validate function you would need to load the image on the selected media entity and then check its dimensions. If they're not correct then set an error

Alimba avatar
in flag
This is not working, cause the validation is getting fired once you open the media library to select an image, at this point there is no media selected in order to validate it. Then it is not fired again even after you save the content. Ideally, would be either to implement a rule to media library of not show images that are smaller than the size that the field should use or trigger the rule upon saving the post.
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.