Action needed for the "After/before deleting a post" trigger

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by kirilius on January 22, 2009 at 4:52pm

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

Posted by gmclelland on May 6, 2009 at 3:47pm

I'm also facing this issue and would like to know the best way of handling it.

hook_nodeapi('delete')

Posted by markus_petrux on May 7, 2009 at 8:03am

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.

Posted by gmclelland on May 14, 2009 at 2:56pm

Wow, thanks for the code. That really gives me a step in the right direction.

Has this been resolved

Posted by Adam S on July 31, 2011 at 4:28pm

Has this been resolved properly in Drupal 7?

Marine job board with Drupal 7 at http://windwardjobs.com

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /