5

I have a product node type and a review node type. The review node type has a node reference field that points to product. I can make one product and many users can add through using the node reference URL widget several reviews. I've been deleting the products while the reviews have been sitting orphaned in the database. This hasn't been a problem till I installed the Search API module. Perhaps the error being caused by the Entity module is a bug but those pesky node references have no business existing if the product has been erased or at least they shouldn't be published.

I can think of many solutions to this including never letting a product be deleted so that all content stays in the database till the end of time.

However, I would like to just delete all the reviews when a product is deleted. How do I do this.

3

This situation is exactly like deleting comments which are entities when deleting the node they are attached to. So I looked at the code in comment.module to see how it is handled there. Here is my solution:

/**
 * Implements hook_node_delete().
 */
function helper_node_delete($node) {
  if ($node->type == 'product'){
    $nids = db_query('SELECT entity_id FROM {field_data_field_review_reference} WHERE field_review_reference_nid = :nid', array(':nid' => $node->nid))->fetchCol();
    node_delete_multiple($nids);
  }
}

This also means that I can test to see if the node type is a review so all node types that reference the review can be handled too since hook_node_delete is recursive in this instance.

2

Install Views Bulk Operations. Create a view (let's call it referencing_nodes) that will select the list of nodes referencing the Product passed in argument.

View type: Node
Add argument: Content: Node reference
Style: Bulk Operations
Selected operations: Delete node
Add field: Node ID

Use Rules to fire an action on product deletion:

Event: After deleting content
Content has type: Product

Add an action: Execute a VBO programmatically on node
View: referencing_nodes
Operation: Delete node
View arguments: return array($object->nid);

Tested, worked! However, it would be great if Rules could cascade and also delete nodes referencing the nodes being deleted by the VBO. Unfortunately, I only get a debugging message saying that's not gonna happen to prevent recursion... Fair enough. :-)

  • How do I do this with Views3 in Drupal 7? – Adam S Jul 31 '11 at 16:36
  • You should be able to get both VBO and Rules to work in D7 and Views 3. The setup would be pretty much same then. – Jan Tomka Jul 31 '11 at 22:44
  • I was running into a problem where the Field API can not find the referenced node and throws errors. – Adam S Jul 31 '11 at 23:04
  • I'm going to need more information than that. Also, note that both VBO and Rules are in beta for 7, Views are an -rc, so there still may be quite a few problems around. Try to google the error messages and have a browse through the issue queue of the modules, too. – Jan Tomka Jul 31 '11 at 23:21
1

This handy little function will delete all children nodes that reference a parent node when you delete it

Helpful if you have multiple users adding content to your site.

Or if a workflow of attaching many nodes to one node has a short life cycle. For instance, if a system of petitions attaches requests to it, it is helpful to be able to delete all requests when you delete the petition.

Create a simple module with the code below

In the info file:

name = mymodule
description = Utility module that performs operations on nodes
project = Custom Modules
core = 7.x

In the module file:

/**
 * Implements hook_node_delete().
 */
function mymodule_node_delete($node) {

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')->fieldCondition('field_referencing_parent_node', 'target_id', $node->nid);
  $results = $query->execute();

  if (!empty($results['node'])) {
    $nodes = array_keys($results['node']);
    node_delete_multiple($nodes);
  }   
}

Replace field_referencing_parent_node with the field that references the parent node. Replace mymodule with the actual module name. Entity Field Query will do the rest.

Source: bradallenfisher comment on How to delete orphaned entity (node) references at Drupal.org

0

You may try Delete Orphaned Terms module for cleaning "orphaned" taxonomy terms that are no longer referenced by any node (orphans). This can be configured automatically (cron) according to several parameters with support for whitelist/blacklist.

WHITELIST AND BLACKLIST LOGIC

- Term pruning: [YES/NO]

  whitelisted ----> blacklisted ----> is tag (t) ----> tags only (T) ----> [YES]
       |       no        |       no      |         no       |          no  
   yes |             yes |           yes |              yes |
      \_/               \_/             \_/                \_/
     [YES]              [NO]           [YES]               [NO]

  In boolean logic: (w OR (NOT b AND (T => t)))

- Vocabulary pruning: [YES/NO]

            blacklisted ----> is empty (e) ----> [NO]
                 |       no       |         no
             yes |            yes |
                \_/              \_/
               [NO]             [YES]

  In boolean logic: (NOT b AND e)
  (Yes, the whitelist and tag pruning have no meaning here.)

Source: README.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.