1

I would like to develop a plugin to help better maintain data integrity between related records. Something like the following:

  1. When deleting an entry, check whether the entry has "reverse related entries" (i.e. other entries that relate to this entry), and if so, interrupt the delete process and show a warning — something like: "This entry is currently being referenced by 'xyz' entry. Are you sure you want to delete this entry?". If the user selects 'cancel', then cancel the delete process. (Perhaps in a future version of craft this should even happen by default if the entries field is 'required' on the reverse related entry.)
  2. Even better, would be an option to cascade delete "reverse-related" entries, with a message that said something like: "Are you sure you want to delete this entry and all related data?", which would then initiate a cascading delete process. Obviously you would want to specify which 'related entries' fields will initiate the cascade — perhaps a checkbox on the 'related entries' field settings that read "when deleting related entries, provide an option to also delete this entry".

From what I understand, as of a recent update, I believe we can now stop processes from within an event method (i.e. onBeforeDeleteEntry). So I can see how one might develop the first option fairly easily.

The second option is a bit more complicated, without also developing a 'custom related entries fieldtype'. Something that I would prefer to not do (to maintain future compatibility). Perhaps a separate panel that has a drop down to select from 'all related entries fields', those that should initiate a cascade, that can be used as a lookup table within the onBeforeDeleteEntry event?

I'm just curious if anyone has attempted anything like this? And/or if anyone can recommend a good approach.

2

I think you've pretty much nailed the options.

1) Seems completely doable and relatively easy as of Craft 2.3 just like you described.

2) Is more difficult. You could go a custom field type route where you have settings defined which define which entry fields the cascading entry deletions occur on. You could also probably cram your own modal/dialog choices into that event to present to the user for specific cascading deletion options (which is what I think you're also describing).

It probably be a lot easier if you didn't need to be concerned about which fields it happened to and you could go scorched earth and just recursively delete all reverse related entries regardless of the field or depth. But I doubt that's the desired behavior in most circumstances.

I'd also be curious to see what an example of the use-case for this would be. My limited imagination is having a hard time coming up with something.

  • Thanks Brad. The use-case here is a gallery/print studio with an art print club (kind of like a wine club) where members get to make selections from a monthly portfolio. In all we have clubs (in multiple countries/languages), subscription plans, subscriptions, orders, users, accounts, portfolios, prints, and artists; all of which are related in one or more ways. If the client deletes any piece of the puzzle it really has a cascading affect: things break or become orphaned. But more generally, I think it would be useful anywhere a related entries field would be considered 'required' data. – Douglas McDonald Jan 30 '15 at 16:23
  • Ahh... makes sense. Would be interested in seeing what you come up with. – Brad Bell Jan 30 '15 at 16:59
  • I'd like to do something similar, but it doesn't look like we can use preventAction on all of the events, including onBeforeDeleteEntry - buildwithcraft.com/docs/plugins/… although it is on onBeforeDeleteDraft. Could this be added? I'd like to be able to protect certain entries from deletion by anyone under certain circumstances. – Mike Pepper Feb 3 '15 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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