I am trying to build a simple hierarchy of two node types. A-type is the parent and B-type is the child. I am also trying to strictly enforce that hierarchy, i.e.:
1) a B-node cannot be created without specifying a parent A-node
2) when an A-node is deleted, all child B-nodes MUST be deleted (with an appropriate warning of course)
I added a nodereference field to my B-type that references the A-type and made that fileld mandatory. This automatically solves my 1st requirement. My questions is: how to enforce my 2nd requirement to delete all referencing child nodes when a referenced parent node is deleted? I searched long but couldn't find a module that does that. What is the best way to accomplish this? Any modules/code snippets?
Thanks!
Comments
Hey there, I think you want
Hey there,
I think you want to look into creating a module of your own. There is a function called hook_nodeapi which you will want to implement, which allows you to perform actions when operations are performed on nodes, including deletion. I doubt there is a module which does exactly what you want yet, although there MAY be a way to do it with the Actions module.
Dave
hm
with rules you would be able to do it, if there is only one children for each node. The problem is that as of now rules can't handle "lists of nodes" like you have in a node reference, when there are multiple nodes. (there is some work in this direction though). But you still can use a php action to do it...
I have multiple child nodes.
I have multiple child nodes. It's in fact a typical gallery situation: one gallery node has many referencing images and I would like to delete the images when the gallery is deleted because otherwise I will end up with inconsistent site - lots of orphaned images (with a mandatory nodereference field pointing to a non-existing node).
You mentioned actions - I am not familiar with them, I saw there are some pre-configured actions in Drupal 6 but non of them is about deleting nodes.
Code to borrow
Views Bulk Operations could have some code you could borrow: http://drupal.org/project/views_bulk_operations
Cheers,
Bill
FunnyMonkey
Click. Connect. Learn.
Using Drupal in Education
FunnyMonkey
I too would like to know how
I too would like to know how to do this. If I remember correctly the Node Hierarchy module does this well. It shows a listing of the nodes that will be deleted right before the parent node is deleted.
The problem with Node Hierarchy
Is that (as I found out today) it creates invisible menu items for each of the child nodes. I was using Image FUpload to create multiple children images which had an image gallery node as their parent. When I got to three galleries with 200 images in each I had the following error:
Warning: Got a packet bigger than 'max_allowed_packet' bytes query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (1, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:1759385:\"Got a packet bigger than 'max_allowed_packet' bytes\nquery: UPDATE cache_menu SET data = 'a:2:{s:4:\"tree\";a:3:{i:1360;a:2:{s:4:\"link\";a:38:{s:14:\"load_functions\";s:26:\"a:1:{i:1;s:9:\"node_load\";}\";s:16:\"to_arg_functions\";s:0:\"\";s:15:\"access_callback\";s:11:\"node_access\";s:16:\"access_arguments\";s:29:\"a:2:{i:0;s:4:\"view\";i:1;i:1;}\";s:13:\"page_callb in /var/www/vhosts/p8dev.co.uk/subdomains/photos/httpdocs/includes/database.mysqli.inc on line 135
at the top of the page for adding the parent gallery nodes.
It appears that Drupal can't handle large menus so it was back to the drawing board for me! If anyone knows an easy way to delete the child nodes automatically when deleting the parent I'm all ears!
@fago - Just curious, is what
@fago - Just curious, is what you described now possible with Rules? I can see this as a very common use case that a lot of people would want to know how to do.
Load a view that lists all the referenced nodes in a node reference field, iterate over the view results deleting the associated nodes?
For example, Image Gallery and Image content types. Say you want to delete all the images referenced by the image gallery when an image gallery is deleted.
If anyone wants to give their input, a similar discussion is here: http://groups.drupal.org/node/155384 - Delete Referenced