I am looking for an advice on what to do in this situation:
- I have a node type called A (parent)
- I have a node type B (child), which has a mandatory CCK nodereference field, that points to a node of type A
- The resulting hierarchy looks like:
A1
|-B1
|-B2
|-B3
...
each B-node points to a parent A-node
The problems is that there is nothing that stops the user from deleting a parent A-node. In that case all referencing B-nodes will be orphaned - i.e. having a mandatory nodereference field that points to nowhere ;-(
I should think that the response to such a situation should be one of these:
1) Don't allow the deletion of a node if referencing children exist
2) Automatically delete the children if the parent is deleted
CCK does not do either of those. Nor I have seen a module that addresses this kind of functionality.
So my question to those using nodereference is: how do you handle orphaned nodes? What do you do to prevent a node from becoming orphaned?
Comments
_
#1 - i would think you could do a hook_form_alter and add some logic to only display the delete button if the reference is blank.
#2 - you could probably do this with actions/triggers and/or the rules module.
===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz
So I figured but the problem
So I figured but the problem is that I cannot code for Drupal. I was hoping to have this functionality in a already available module.
_
For handling it with #2, i don't think you'd need to code.
===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz
Not sure if that's the case.
Not sure if that's the case. In D6 I enabled Triggers module and when I go to the Triggers config page I see the following message for the "After deleting a post" trigger: "No available actions for this trigger."
This means that I have to code an action that deletes all referencing nodes and somehow make it available for that trigger. Actually this trigger won't do the job because it is a post-action trigger, i.e. it is AFTER deleting a node. But after I delete a node, how should I know its ID in order to look it up. It rather has to be a BEFORE delete trigger, but such is not available.
_
I just took a closer look at this and you're absolutely right-- so it looks like it does require a module that implements hook_nodeapi on the delete operation to delete the children before the node itself is deleted. I doubt if it's more then a dozen lines of code-- if I get a chance to work it out i'll post back.
===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz
Thanks very much! I was just
Thanks very much! I was just looking at hook_nodeapi myself but I don't know Drupal's and CCK's internals so I don't know how to select all referencing nodes for a given $node.