Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Specify the name or path of upload

default discord avatar
pachachicha2 years 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

  • discord user avatar
    dribbens
    2 years 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.

  • default discord avatar
    pachachicha2 years 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

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.