0

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?

2 Answers 2

0

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

1
  • 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.
    – Alimba
    Jun 14, 2023 at 14:02
0

Not an answer (yet) but there is a Drupal core issue filed on Drupal.org to fix this:

Image minimum resolutions should be configurable per field instance (for fields of Reference type Media with Media type Image) and these should be honored when uploading new images or selecting existing ones from the media library [#3016649]

Anything that helps you develop a workaround would likely help develop a patch, or developing a patch may be the only option, so worth engaging people following that thread in your work.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.