This patch adds the functionality which will automatically maintain the back reference of a shared nodereference field.

So when creating a node reference from node a to node b, this will create a back reference from node b to node a.

This will also maintain revisions so that if you create a revision on the link between node a and b, a new revision will be create on node b for the reference to node a.

Comments

gordon’s picture

StatusFileSize
new3.59 KB

Found a small bug in the setting for the option.

manimal’s picture

the patch fails for me, I can't get it to work.

Hunk #1 FAILED at 1.
Hunk #2 FAILED at 37.
Hunk #3 FAILED at 87.
Hunk #4 FAILED at 130.
Hunk #5 FAILED at 581.

gordon’s picture

I have checked it and other than the first hunk with is the $Id$ it patches fine.

Make sure that you are applying it to the correct version.

onefeatureman’s picture

subscribing

onefeatureman’s picture

Tell me - what I need to have in the referenced node for the bi-directional reference to work? A counter-reference? How the patch is applying the reference?

thanks

nikola

gordon’s picture

Basically it looks on the referenced nodes for the same field and then adds the reference back to the current node.

If you are referencing a different node type then you need to share this field onto the other node type.

onefeatureman’s picture

Ok, I see the point.

I expected it to behave differently, mainly because of my use for it.
I have a content type music holding an mp3, and there I have an array of nodereference to musician content type.
I thought it would be great if when I reference musician with musician nodereference - in the same time musician references music with MUSIC nodereference. (where I can define which fields are to be appended to automatically - in this case the MUSIC nodereference field)
This way I could keep it organized and clear for everybody.

Why? I have more than one node reference field in every content type, and so they have specialized names and usages. If the bidirectional reference will force me to use the same field for the two contents (of which the referenced one do not even need this kind of field, because the whole node is about the field - that's why I am referencing it :) ) it will make a mess in my plan...

Maybe its pointless from Your point of view, but is it possible to code a widget to choose a field to back-reference in the referenced node in the field options?

Mac Clemmens’s picture

@onefeatureman -- Interesting point #7 - I was wondering the same thing...
-subscribing-

dsel’s picture

subscribing

dsel’s picture

subscribing

dan_aka_jack’s picture

this sounds like a very useful feature. Subscribing.

Anonymous’s picture

+1 subscribe

Would love this as a standard feature. Especially for the 6.X version....

chaps2’s picture

Version: 5.x-1.6-1 » 6.x-2.x-dev

Here's a way to do this in 6.x. It uses the rules module and doesn't require any patching. I used the latest dev version of rules (dated march 31). I'll try to check with the latest release.

Basically you need to set up two triggered rules that update the nodereference field on the referenced node to the nid of the node that is referring to it. Two rules because I couldn't find a single event that would work for both node creation and node update.

Node creation rule

  • Create a new triggered rule with the event set to 'After saving new content'.
  • Add a condition of type 'CCK > Field has value', select your nodereference field, check the negate box, then enter the following php code:
    return array(0 => array('nid' => null));
    This condition will return TRUE when the nodereference field has been set. Save.
  • Add an action of type 'CCK > Load a referenced node'. Make sure the field is set to the nodereference field that you set in the condition. Other settings should be OK. Save.
  • Add an action of type 'CCK > Populate a field'. In the content drop-down select 'Referenced node'. In the field drop-down select the nodereference field as above. Enter the following php:
    return array(0 => array('nid' => $node->nid));
    Set weight to 1. Double check the content drop-down select is set to 'Referenced node' as it may reset itself. Save.

Node update rule

  • Go back to the triggered rules page and clone the rule you just created. Enter a new label and change the event to 'After updating existing content'. Save changes.
  • Edit the condition and delete it.
  • Add a new condition of type 'CCK > Field has changed'. Select your nodereference field in the field drop-down. Save.

And that should do it.

alanburke’s picture

Could you export those rules.. Would be useful to make a start.

chaps2’s picture

StatusFileSize
new7.17 KB

@alanburke - If you think it would help...

The attached contains the exported rules (2 of them) created as per #13 for a 'Person' content type with type name 'nu_profile'. The rules maintain bi-directionality for the node reference field called 'field_spouse_partner' (which refers to content of the same type selected from a view).

If you had a similar requirement you could do a search/replace and import but it's pretty easy to do from scratch via the UI.

alanburke’s picture

@chap2 - Thanks.
I managed to get it working from the UI.
It was taking me a while to get my head around the Rules modules,
but is so powerful.

[In much the same way that 'views is the answer' to many Drupal questions,
I anticipate that 'Rules is the answer' will become a common answer in the future!]

gagarine’s picture

tracking

soulston’s picture

Does this need the token module installed to funciton?

I have tried to get this to work but no links are placed in the nodes that I am referencing

chaps2’s picture

If you haven't already, try enabling the core module "PHP filter". On each rules action configuration form there should be a "PHP Evaluation" section that lists the variables that you can use.

alanburke’s picture

New module which promises this functionality.
http://drupal.org/project/cnr

soulston’s picture

I tried this out and it seems to work fine. I have it set with three different content types which each reference each other.

Has anyone tried to use this for a large number of nodes?

I need to implement something with maybe 1000 nodes and up to 250 bidirectional links. I see the usage stats is at 18 as of now.

Is this kind of use going to create a performance hit and if so should I be looking for a better solution?

extect’s picture

There is one problem with the solution in #13: When you delete your nodereference, it does not automatically delete the reverse nodereference on the referenced node as well. Any idea how to solve this?

nickbn’s picture

Will the rules method above work with multiple node references? For example, a node that references any number of other nodes?

markus_petrux’s picture

This pattern duplicates information in the database which is redundant and is prone to create integrity issues. On the other hand, it is not so complex to create a view that displays back references doing the proper join with the node reference field and the node data of the referred node. But this probably requires Views, and I believe it is a complex goal at this point in time for CCK in D6. Hence, I would say "won't fix" or "postponed" here. IMHO, at least in D6, this needs to be resolved by contrib modules.

I'm sure there are a few solutions in contrib to resolve this, but I think they also duplicate data to get the back references. So I have written a module that, among a few other things, provides back reference views automatically, where views can be easily adapted. These back references can then be rendered using several methods in the node view.

http://drupal.org/project/noderelationships

markus_petrux’s picture

Status: Needs review » Closed (won't fix)

Well, I think this won't get in.

You may wish to check out the following issue for follow ups on the reverse node reference feature, which is something that will be possible in contrib as well.

#241078: Reverse node-reference views relationship

neil.brown’s picture

Subscribing.

Anonymous’s picture

/me sub

Gabriel R.’s picture

I am a bit torn between the various solutions provided here.

quinn-2’s picture

sub