I'd like to have referenced nodes (child) disappear from certain views and am struggling to figure out what the easiest way to do this is? I have the feeling this could be done by updating the child node, when the parent node is submitted, but don't know where to start. Does someone have a code snippet or resource that would help me add this feature? Any hints are appreciated.
Thanks so much, Chris

Comments

challer’s picture

Just in case anyone has a need for updating referenced nodes, here's my code (originally taken from the email update fieldaction):

/**
 * Implementation of a Drupal action.
 * This action sets referenced nodes as unpublished.
 *
 */          
 
// set referenced node unpublished

            
function action_set_unpublish_nodereference($op, $edit = array(), $node) {
  switch($op) {
    case 'metadata':
      return array(
        'description' => t('Set referenced nodes as unpublished'),
        'type' => t('Unpublish'),
        'batchable' => false,
        'configurable' => true,
      );

    case 'do':
      // does this node have the node reference field?
      if (array_key_exists($edit['node'], $node)) {
        // yup, make sure it is an array so we can foreach it
        if (is_array($node->{$edit['node']})) {
          // pull out each represenation of the field (an array with nid) in it
          foreach ($node->{$edit['node']} as $field) {
            //a nonexistant nid field means that the nodereference box was not used
            if (!isset($field['nid'])) continue;
            
            // unpublish node referenced in the field
            db_query("UPDATE {node} SET status = 0 WHERE nid = %d", $field['nid']);
            watchdog('merge', t('%field unpublished due to merging.', array('%field' => $node->title)));
            drupal_set_message('Successfully merged');
          }
        }
      }
      break;

    // return an HTML config form for the action
    case 'form':
      // default values for form
      if (!isset($edit['subject'])) $edit['subject'] = '';
      if (!isset($edit['message'])) $edit['message'] = '';
      $form = array();

      // add form components
      $form['node'] = array (
        '#type' => 'select',
        '#title' => t('Node Reference Field'),
        '#default_value' => $edit['node'],
        '#options' => _fieldactions_nodereference_fields(),
        '#description' => t("Select the node reference field."),
      );
      return $form;

     // validate the HTML form
    case 'validate':
      // no errors possible now
      return true;

    // process the HTML form to store configuration
    case 'submit':
      $params = array(
        'node' => $edit['node']);
      return $params;
  }
}

/**
 * Get a list of all the node reference fields
 */

function _fieldactions_nodereference_fields() {
  $allfields = content_fields();
  $fields = array();
  foreach($allfields as $name => $field) {
    if ($field['type'] == 'nodereference') {
      // if a field is used twice, we only need one label, but we append
      // the field name to remove ambiguity about where this will apply
      $fields[$name] = $field['widget']['label'] . ' (' . $name . ')';
    }
  }
  
  return $fields;
}
amitaibu’s picture

Maybe Reference By Views filter module can help you. It actually tells the Views module to treat the referenced nodes as the base table.

karens’s picture

Status: Active » Closed (won't fix)

A very old support request for D5, won't do anything with this now.

salberto25’s picture

I also need that functionality...unpublish a node in a view when that node was referenced...anyone??