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
newsPostsand these are used as a reference on the
homePage. A user can select what
newsPoststhey wish to show on the
homePage.
If a user removes a
newsPost, it breaks the
homePageas 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?
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.
I have implemented this leveraging the searchPlugin to index any relationships of any kinds of documents. Then a
beforeDeletehook 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:
Star
Discord
online
Get dedicated engineering support directly from the Payload team.