I realy love Drupal and especially CCK related stuff. But the best of all is referencing one information (node) with any other in a structured way. CCK with nodereference does a wonderful thing to help me build prototypes and production sites that are more than a CMS, they are becoming real Knowledgebase Applications and much more in the future.

To be able to see all reverse links that point to the node that is actually viewed, I decided to write a php snippet for a block. The block now display all referrer nodes and groups them by node-type.
what it does:
- detect the nid of the actual viewed node
- go through the referrers
- group the output by node-type (this is accidentially sorted at the moment)

It is all not clean and I could never get through all the array stuff that would be needed to 'really' sort the items, but I hope that someone of the realcoders can take this idea andput it inside nodereference.module that a standardized block for this exists.

Additional wishes:
- I would like to add other fields of the referrer-node, but dont have the knowhow of cck module how to get it with a snippet. Has someone a map of all the wonderful array monsters that I maybe never can fight with my littlephp-snippet sword? ;-)

Please find in the attached zip file the php-snippet and a Screenshot.

<?php
global $user;

// identify node id for current viewed node
function nid(){
  static $nid; if(isset($nid)){return $nid;}
  $q = $_REQUEST['q'];
  $p =  drupal_get_normal_path($q);
  $nid = array_pop(explode("/",$p));  
if (substr($p,0,4)<>'node') {$nid=0;}
// check if it is a node,
// and not a argument number at the end of the path
  return $nid;
}
if (nid()>0) {

$referrers = nodereference_get_referrers(nid());

$content_type_grouping = '';

      foreach($referrers as $referrer) {
         // group output by node type
         if ($content_type_grouping <> $referrer->type) {
             $content_type_grouping = $referrer->type;
             $output .= '<br>'.substr($referrer->type,8).'<br>';
         }
         // Check Users View Permission
         if (node_access('view', $referrer->nid, $user->uid)) { 
            $output .= '<a href="'.$referrer->nid.'">'.$referrer->title.'</a><br>';
         }
}
      return $output;
}
?>

Comments

biohabit’s picture

Title: Reverse Link for all nodereferece connections » white screen of death

I'm using the CCK for almost the exact same purpose, but when I enable the above PHP code and go to a node that includes nodereferences I get the white screen of death. Any ideas?

biohabit’s picture

Title: white screen of death » what version of CCK was this developed against?

What version of the CCK was this developed against? I'm using the CVS from June 12th.

biohabit’s picture

Title: what version of CCK was this developed against? » display nodereferrers in a block

How the heck was I able to rename this ticket when I'm not the owner? I don't remember the exact title so this was a guess.

robert.redl@easytouch.cc’s picture

Title: display nodereferrers in a block » Reverse Link for all nodereference connections

Thanks for your feedback,

I am using for this at the moment
- Drupal Version 7.2,
- CCK cvs Version
/modules/content.module Version 1.64 2006/06/12 19:36:54 JonBob Exp $

With Drupal 4.6.x I have to figure out how the actual node-id can be fetched.
Should be something like $node->nid. If I find time, I will check this out for the 4.6 Users.

if (nid()>0) { REMOVE this line and the function aboe,and in the the last line the closing }

$referrers = nodereference_get_referrers($node->nid); 

The title change was maybe possible, because no owner was set? I also dont know the miracles of the issue system? ;-)

robert.redl@easytouch.cc’s picture

Version: 6.x-1.x-dev » 4.7.x-1.x-dev
StatusFileSize
new3.1 KB

OK, The above example for the block works only with the following Versions:

1. CCK Version Drupal 4.7.0
2. The followingPatch must be applied to the file /modules/cck/nodereference.module
http://drupal.org/files/issues/nodereference.module_5.patch

Apply like this (if you have Linux Shell access with CVS
# patch nodereference.module < nodereference.module_5.patch

See the attachment to thiscomment for the already patchednodereference.module

robert.redl@easytouch.cc’s picture

If you want to use this with
- CCK Module CVS version
You have additionaly to patch nodereference.module, to add the following function to the end of
content.module

/**
 * Return a list of all fields.
 *
 * @param $reset
 *   If TRUE, clear the cache and fetch the information from the database again.
 */
function _content_fields($reset = FALSE) {
  static $fields;

  if ($reset || !isset($fields)) {
    $fields = array();
    $field_result = db_query('SELECT * FROM {node_field} nf');
    while ($field = db_fetch_array($field_result)) {
      $global_settings = $field['global_settings'] ? unserialize($field['global_settings']) : array();
      unset($field['global_settings']);
      $field = array_merge($field, $global_settings);
      $instance_info = db_fetch_array(db_query("SELECT type_name, label FROM {node_field_instance} WHERE field_name = '%s'", $field['field_name']));
      $field['widget']['label'] = $instance_info['label'];
      $field['type_name'] = $instance_info['type_name'];
      $fields[$field['field_name']] = $field;
    }
  }

  return $fields;
}

It seems thatsomeonehadlost the function from CCK 4.70 up to CVS, but a lot of patches and sandboxes I have seen the last days use it. Let's wait, until the Pro's sort this out and get all the interesting referrers and date modules on the track. CCK and its newest patches really rules!

karens’s picture

Status: Needs review » Closed (duplicate)

There is another issue with more current activity on this topic at http://drupal.org/node/60756, so marking as duplicate.