Question

The node-reference module (a component of CCK) provides a great way to create a reference one node from another. What is the best way to expose a two-way relationship between the two? Example below:

For instance, in a ticket tracking system with two tickets (nodes) A & B, you could create a node reference from ticket A --> B and mark B as a child. When viewing ticket A it would be trivial to display this information. However, I would also like to display this relationship when viewing ticket B and show ticket A as its parent.

This same requirement would also apply in non-hierarchical situations (tickets C & D) with a reference from C --> D, the relationship should be exposed on ticket D as well as on C.

Was it helpful?

Solution

There are many modules available, your milage may vary:

BackReference (Drupal 6 only)

NodeReferrer (Drupal 7 beta release)

Node Relationships (Drupal 6 only)

Corresponding node references (Drupal 7 stable release)

Corresponding entity references (Drupal 7 dev release)

Content Dependency (Drupal 7 stable release)

OTHER TIPS

Entity Reference Module now works with Corresponding Entity References. This is ideal for all nodes and non node entities.

For Drupal 6, the best option is probably the NodeReferrer module. For Drupal 7, Relation fits the bill but is not yet ready.

If you're using Drupal 7 / Views, you can build a 'reverse' relationship to get a listing of nodes that refer to a given node (or, perhaps, users that refer to a given node). Check out the patch in this issue: http://drupal.org/node/1043260

In my opinion, unless you need the explicit, pre-built reverse relationship stored in the database (this can be sometimes be helpful for performance reasons), using one of the other solutions is overkill. You can always look up a relationship in your own SQL query if you need to...

[Edit: Since originally writing this post, the patch in the issue above has been committed to References for D7, and should be the preferred way of backreferencing (works great when simply needing lists of nodes or users referencing a certain other node or user).]

[Edit 2: If you're using Entity Reference, it looks like the preferred solution is to use Corresponding Entity References, and there's also the less-widely-used Entity Reference Field Synchronization.]

In Drupal 7 (which you indicated) it seems the solution is to use the Entity Reference Module in combination with the (Entity)Reference Field Synchronization module which adds the necessary functionality to keep the relationships synched.

The same can be achieved with References Module with cnr module - this solution works (I'm using it on one Drupal 7 site currently), but is becoming obsolete/deprecated in favor of the first solution I gave.

All the other solution mentioned in the above answers are relevant only for Drupal 6 (except for Relation which has the potential to become a great module, but right now, doesn't really do much and has lots of issues).

Alternatively, if you're working on a custom implementation and want a straight forward way of referencing back, you can hook into nodeapi, listen for the save operation and than programmatically update the referenced node.

You can also try the Reverse Node Reference module.

Simply use Rules and this rule http://drupal.org/node/1836240

You can use the Content Dependency module.

It provides you reverse references via tab and supports following modules: node reference / entity reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top