2

In Rules on Drupal 7, how do I set up a rule / ruleset so when a node is saved compares a textfield from the saved node with a textfield in all nodes of a content type and if match, not the saved node but the matching node should be unpublished?

2
  • What exactly are you trying to do. This won't be very easy with Rules without writing custom PHP code. Does the unique field module cover your use case?
    – Thomas4019
    Nov 28, 2013 at 22:05
  • No, I don't think unique field works for my use case. My use case is: I use feeds to import two kinds of articles into two different content types. When a article is imported to content type A and there is a ID match on a field in content type B - the article in content type B should be unpublished. Dec 3, 2013 at 16:07

2 Answers 2

0

I am not sure this can be accomplished with rules entirely. I am interested if a solution exists. Since I am much more comfortable with "custom module" solutions, I will give you some pseudo code that might help you achieve this functionality.

Imagine you are creating a custom module called "mymodule"

function mymodule_node_update($node){
  //get content type
  //if content type="x"
     // run entity_query(because it has field condition property) to get all PUBLISHED nodes where field Y == node -> field Y. and store the node id's (shorthand: NIDS).
  //save all the NIDS into an array 
  // run db_update query to set the status to unpublished
}

how to use entity query Regards !

Oleg

2

I haven't found a solution using only rules yet, maybe it would be possible with rules and views bulk operations (vbo)? A custom module is what I did to solve it (thanks Oleg for pointing me to EntityFieldQuery). Here is the code:

<?php

/**
 * @file
 * custom_unpublish.module
 *
 * This module compares a field in a node when updated with a field in all 
 * nodes of a content type and if match, the matching nodes is unpublished
 */

function custom_unpublish_node_update($node) {
  // Only check nodes of a certain content type
  if ($node->type == 'article') {
    $wrapper = entity_metadata_wrapper('node', $node);

    // Get the wanted field
    $id = $wrapper->field_id->value();

    $query = new EntityFieldQuery();

    // Check only a certain content type (bundle) and make a fieldCondition
    $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'accepted_article')
    ->propertyCondition('status', 1)
    ->fieldCondition('field_id', 'value', $id, '=');
    $result = $query->execute();

    if (isset($result['node'])) {
      $result_items_nids = array_keys($result['node']);
      $result_nodes = node_load_multiple($result_items_nids);
      foreach ($result_nodes as $result_node) {
        // Unpublish the node
        $result_node->status = 0;
        node_save($result_node);
        watchdog('custom unpublish', 'Match for ' . $id . '. ' . $result_node->title . ' was unpublished.');
      }
    }
  }
}
1
  • Good solution. :) I like the line with array_keys. Have to use it. Just a hint, use a try catch construction with all kinds of queries, since sometimes the query is not properly executed and can lead even to WSOD (you never know). Dec 5, 2013 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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