Like what we’re doing? Star us on GitHub!

Specify the name or path of upload

Daniq
4 weeks ago
2

I have a 2 collections first is for products with preview image field (upload type) in it as well as field for other images of the product, second one to store those images -

productMediaCollection

.


I wanted on image upload (for example from

products

collection) to change the name or/and file path of it to fit product title like:



productMediaCollection/someProduct/image_uuid.webp;

or at least to change the name to something like this:



productMediaCollection/productTitle_uuid.webp

So is there a way to change default uploading name for example via

beforeValidate

hook, because i dont quite understand how to do it?


Or maybe it possible to provide some function to

staticDir

in

productMediaCollection

to make it conditional?



Thank for answers

  • dribbens
    Payload Team
    4 weeks ago

    There is a Github discussion on this topic:

    https://github.com/payloadcms/payload/discussions/1153

    You should be able to handle it in a

    beforeOperation

    similar to marked answer.

  • Daniq
    4 weeks ago

    Thank, this solution might work, but if i create hook on

    productMediaCollection

    then i cant have an access to information about product from which i created a media 😦


    so i cant provide a product title or something like that into hook



    So i media collection hook dont knows from where it was edited last time or something like that

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More