Answers.Body: <p>I think you've pretty much nailed the options.</p>
  
  <p>1) Seems completely doable and relatively easy as of Craft 2.3 just like you described.</p>
  
  <p>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).</p>
  
  <p>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 <em>all</em> reverse related entries regardless of the field or depth.  But I doubt that's the desired behavior in most circumstances.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-30T16:23:04.923Z
Answers.Text: 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.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-30T16:59:42.900Z
Answers.Text: Ahh... makes sense. Would be interested in seeing what you come up with.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T11:51:47.590Z
Answers.Text: I'd like to do something similar, but it doesn't look like we can use `preventAction` on all of the events, including `onBeforeDeleteEntry` - http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeDeleteEntry 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.
Answers.UserDisplayName: Mike Pepper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-30T04:19:24.057Z
Answers.LastActivityDate: 2015-01-30T04:19:24.057Z
Answers.UserDisplayName: Brad Bell
Body: <p>I would like to develop a plugin to help better maintain data integrity between related records. Something like the following:</p>
  
  <ol>
  <li>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.)</li>
  <li>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".</li>
  </ol>
  
  <p>From what I understand, as of a recent update, I believe we can now stop processes from within an event method (i.e. <code>onBeforeDeleteEntry</code>). So I can see how one might develop the first option fairly easily.</p>
  
  <p>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 <code>onBeforeDeleteEntry</code> event?</p>
  
  <p>I'm just curious if anyone has attempted anything like this? And/or if anyone can recommend a good approach.</p>
  
Tags: <plugin-development><relations><entries-field>
Title: Plugin to maintain data integrity between related entries?
ThreadText: # Plugin to maintain data integrity between related entries?
  
  (Asked by: username_0 on 2015-01-29)
  
  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.
  
  **Tags:** plugin-development, relations, entries-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-01-30. Score: 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.
  
  > **Comments:**
  > **username_0**: Thanks username_1. 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.  
  > **username_1**: Ahh... makes sense. Would be interested in seeing what you come up with.  
  > **username_2**: I'd like to do something similar, but it doesn't look like we can use `preventAction` on all of the events, including `onBeforeDeleteEntry` - http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeDeleteEntry 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.  
  
  ---
  
  