I have a node type A (parent) and a node type B (child). B-nodes have a mandatory CCK nodereference field that points to a parent A-node.
A1
|-B1
|-B2
|-B3
Currently it is possible to delete the parent A-node and all child B-nodes immediately become inconsistent (a mandatory nodereference field points to an non-existing parent). CCK does not address this flaw and I need a workaround. Reading through the documentation I found out that it might be possible to create a custom action (in D6) that will delete all child nodes when (before) the parent is deleted.
The problem is that I don't have experience with PHP and Drupal coding and I don't know how to do it. I could certainly use a skeleton action from the documentation and figure out how to delete all children nodes from the appropriate table. However I don't know what else must be deleted in order not to break something else (comments, permissions, etc.)
Does a similar action already exist, which I can leverage on and use with as little customization as possible?
Thanks!
Comments
I'm also facing this issue
I'm also facing this issue and would like to know the best way of handling it.
hook_nodeapi('delete')
Please, be aware that this will delete nodes, so you should make backups and try on a developement environment, etc. ;-)
Here's a skeleton on how it can be done:
<?php
function mycustom_module_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
// If node is parent, then delete children.
if ($op == 'delete' && /* check if it is a parent node here. */) {
// Build an array of child node ids.
$child_nids = something_that_gives_you_child_nids_for_the_given_parent();
// Delete children nodes.
foreach ($child_nids as $child_nid) {
node_delete($child_nid);
}
}
}
?>
Wow, thanks for the code.
Wow, thanks for the code. That really gives me a step in the right direction.
Has this been resolved
Has this been resolved properly in Drupal 7?
Marine job board with Drupal 7 at http://windwardjobs.com