Reverse node reference
kelisfreek - July 25, 2007 - 03:26
So basically, I'm trying to display a list of nodes that references the current node.
I'm making each node about a person. I made node reference fields to enter in each persons mother and father. Now I want to display each person's children based on the mother/father fields.
I was able to get a list of the right nodes using a view to filter nodes based on the nodereference fields... but I have to select a specific parent node to get the list from. I want it to automatically get the right parent node from the current node...
I'm pretty new to drupal and I dont think I'm that good on explaining so sorry if this is confusing... Can anyone help?

Just thought I'd bump this
Just thought I'd bump this up to the top once more... can anyone help?
Yesss!
I finally figured out how to do this for my own site, after a couple of afternoons of gnashing of teeth.
I have nodes for games, and nodes for persons. The person nodes have node reference fields which refer to the game nodes. (I'm using this for people who want to register for games at a game convention. For various reasons I didn't want to use the signup module.)
I create a view with:
$args[0] = arg(1);(Thanks to a comment from Paul Resnick here)I go to the block administration page, activate the block, and choose that it should only be shown on the nodes that hold games.
And voila! At the bottom of my game page, there's a nice little list of all the people who have registered for that game.
Hmm, it occurs to me that it will be a lot of bother to specify where the block shall be displayed for all relevant pages. If I remember correctly, I've seen a module for CCK Viewfields which should automate this nicely. Or perhaps I'm better off with a PHP snippet...
* strolls over to the Handbook, happily whistling *
You are amazing. Thanks!
You are amazing. Thanks!
How do you add a node?
zirvap
How can you add a game on the "person add form"?
Is this possible?
Is there a way to add and reference at the same time?
Thanks for your help.
just gotta say
this code works perfect and zirvap you are a life saver!
multiple node reference arguments
How would you create a this views block that contains multiple node reference arguments?
TIA,
txcrew
Only one
The views blocks I made only had one node reference argument. I made several blocks with one node reference argument each. (Heh, that sounds cryptic even to myself. Tell me if you'd like a more detailed description.)
That was my last resort
In my case, I have about 11 Node Reference fields in my content type (see http://drupal.org/node/201815 for my scenario) and I was hoping there may be a better way to do it. Perhaps just editing that Argument Handling Code to accomdate for multiple for one or more of one of the arguments that are selected. But alas, I have very limited programming knowledge and from what I have been researching so far, it doesn't seem very possible.
Does your method have any impact on performance? Any more or less than having one view with several arguments?
Thanks for the reply!
txcrew
Don't know about performance
I have six node reference fields, which means it wasn't too much of a hassle to make a view for each. I haven't looked at performance at all. The scope of the site is pretty small (a yearly game convention with about 100-150 participants), so performance isn't much of an issue.
Strange views cache issue
UPDATE: Sorry, I figured out the issue myself, 2 of my db tables were out of sync.
THANK YOU VERY MUSH! It
THANK YOU VERY MUSH!
It works for me.
The sample website (Chinese):
http://www.buyerjoy.com/shop/761 and http://www.buyerjoy.com/brand/627 refers each other
--------------------
BUYERJOY
I cannot configure views2
I cannot configure views2 (on D6) following your suggestion. Need I to do in a different way?
Solved :-)
I've solved it on D6 with views2. I wanted to show (on a block) all nodes which had the current node referenced.
This allow me to solve this problem :-)
Simply amazing :-)
Thank you!
I wish I had come to this earlier, Thanks. This is really good.
Can you explain a little more?
I've been reading about this for 6 hours and getting more and more confused. I am very new to php, so maybe that's my problem. I'm using D6, views2. I have a content type called projects, and a content type called testimonials that has a node reference to projects. I want to use views to create a block on the project node pages that displays all the testimonials for a particular project. I tried the above, plus many other guesses, some of which were informed by reading about D5 solutions, but nothing is working.
Any help would be greatly appreciated.
Yesss!
Deleting duplicate post.
Yes?
Yes it can be done? How would you do it?
CCK Nodereference
Use CCK to add a Node Reference field to the 'person' content type, then when you create 'person' content you'll be able to select an existing node to reference.
The problem is that you
The problem is that you can't add a person from the create game form or vice versa. You can only reference a record if it has already been created.
I sort of have the same problem here and here.
Maybe someone could help me out.
These are very relevant:
These are very relevant: http://drupal.org/node/60756 http://drupal.org/project/nodereferrer http://drupal.org/node/158287
Drupal 6
Anyone have it working on Drupal 6?
I would like to know how to
I would like to know how to do this on drupal 6 too?
thanks
Luis
I would like to know how to do this in drupal 6 too
Argument: Node Reference does not show as one of the options. Thanks
http://www.faunapolis.org/
How to reverse node reference in Drupal 6
I asked the CCK module developer, and he very promptly answered.
The newer development version has the solution, please see:
http://drupal.org/node/265958
the alpha version of CCK for D6 did not have the option incorporated yet
http://www.faunapolis.org/
Working!
Based on http://drupal.org/node/259845, finally got it working.
1. Created a relationship,
2. Argument Node:nid, using relationship,
3. Also had to use page/attachment type of view as blocks can not pass arguments, as far as I know,
4. Did other settings, like fields, filters, etc,
5. Used
<?phpprint views_embed_view(my_view_name, $display_id = 'attachment', arg(1));
?>
6. Worked!
Hope this helps someone.
Thanks!
I have been trying to figure out how to basically add the functionality of nodereferrer to Drupal 6 since the module has not been ported yet, and I would have no clue on how to help port it. After tons and tons of searching and fiddling I finally got exactly what I was looking for out of your method. I did not even create a block view, only an attachment since I had no need to have it displayed in a block.
Also just for reference, I was not sure where I was supposed to put the PHP code, but finally figured out that I had to create a .tpl.php file for the content type and add it using this method: http://drupal.org/node/17565
Thank you so much for this.
This may be the solution -> Nodereferrer field
Hey all,
I've been looking for what I think is the same thing, and recently, somebody created it!!! Check this post about the Nodereferrer field in Drupal 6!
Matherion
nodereferrer rocks
i think this is perfect (I'm using drupal 5), works great for me
_
(subscribing)
Code for Views2 in Drupal6
Many thanks for all the hints above. Using the guides above, I've created a very simple view in View2 which displays the "reverse node reference" list in a block. I thought I'd export it here:
<?php$view = new view;
$view->name = 'reverse_node_reference';
$view->description = 'Provides a list of nodes which reference the current node in its node-reference field';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
'title' => array(
'label' => '',
'link_to_node' => 1,
'exclude' => 0,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'field_node_reference_nid' => array(
'default_action' => 'default',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'node',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'not' => 0,
'id' => 'field_node_reference_nid',
'table' => 'node_data_field_node_reference',
'field' => 'field_node_reference_nid',
'relationship' => 'none',
'default_options_div_prefix' => '',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(
'blog' => 0,
'poll' => 0,
'forum' => 0,
'book' => 0,
'event' => 0,
'feed' => 0,
'page' => 0,
'simplenews' => 0,
'story' => 0,
'video' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'1' => 0,
'3' => 0,
'2' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_php' => '',
),
));
$handler->override_option('filters', array(
'status' => array(
'operator' => '=',
'value' => '1',
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'status',
'table' => 'node',
'field' => 'status',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('title', 'Related items');
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
'grouping' => '',
'type' => 'ul',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);
?>
maybe it would be a better
maybe it would be a better choice to move it to the docs
Luis
So far all the success
So far all the success stories here are talking about creating a block view. What about page view?
Based on the info provided by everyone here, I was able to create a block and display it on the current node, with all the referencing nodes displaying correctly.
But I also wanted to show a page with all the referencing nodes based on the current node as well.
For example, I have a Shop and a Product node, with the product node referencing the shop node.
On my shop page (eg. shop/some-shop), I added the block to show all the referencing products, displaying only up to about 9 products. No problems there. But I wanted a dedicated page for each shop to be able to show its referencing products as well.
So I added a Page and had input all the same settings for the block, including a path for the page (shop/%/products). Funny thing is, the page won't show if I enter the url shop/some-shop/products. It only shows if I enter shop/1/products, 1 being the shop nid.
So anyone has any ideas on how to get this to work?
---
www.drupaldiy.com - Showcase your Drupal site
Is this really a reverse reference?
Hey dan_aka_jack,
Thanks for posting your view here. I imported it and tried it out but it didn't do what I was expecting.
Apologies if I just confused but this seems to be doing a normal node reference view rather than a reversed on. If I pass the view the argument of the node that is referenced in the node reference field, the view returns nothing. When I pass the nid of the node that is doing the referencing, the referenced node shows up (just like a normal node reference view would).
So am I missing another step here besides importing your view?
Check this
Check this http://drupal.org/project/nodereferrer
It might do what you want.