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.

Alert user if they are deleting an item that is referenced somewhere else

default discord avatar
mrl72 years ago
2

Hi all, is there a way in payload to alert a user if they are deleting an item that is referenced somewhere else?



e.g. If I have a collection of

newsPosts

and these are used as a reference on the

homePage

. A user can select what

newsPosts

they wish to show on the

homePage

.



If a user removes a

newsPost

, it breaks the

homePage

as the reference no longer exists. I would like to alert or stop the user from deleting that news post until they have removed the reference. Is this possible?

  • default discord avatar
    majohannsen2 years ago

    You could use a beforeDelete hook on the newsPost collection and check if the homePage references this Post. If so you could then throw an error to prevent the post from being deleted.



    But i don't know if this is the best solution bc when there are many collections that can reference this collection (e.g. Media) all these collections have to be checked one by one.

  • default discord avatar
    nooflorist8 months ago

    I have implemented this leveraging the searchPlugin to index any relationships of any kinds of documents. Then a

    beforeDelete

    hook looks for documents having relationships to the to-be-deleted document in the search index, as well as all globals.



    This provides a complete deletion protection usable for all kinds of collections.



    I have detailed a solution here:


    https://github.com/payloadcms/payload/discussions/558#discussioncomment-13656269
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.