20

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.

25

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)

  • Corresponding node references works perfectly for me, and once CER is stable I will switch to that. – user11312 Jun 21 '13 at 17:40
7

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

4

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

  • I always use NodeReferrer module, for what it's worth – electblake Mar 17 '11 at 21:10
  • 1
    The Relation module is now ready, since July '12 – ted.strauss Nov 9 '12 at 19:45
  • Yes, Relation seems to be quite a powerful option. Otherwise, CER is also a good lightweight option. – shivams Jan 17 '16 at 13:27
4

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.]

  • It looks like References is going to become deprecated in favor of Entity Reference. Not sure where that leaves this answer. – Jay Jan 28 '12 at 12:18
  • 1
    References will still be sticking around until D7 is a distant memory... but if you're using ER, you can use Corresponding Entity References to do the same thing as References provides out of the box. – geerlingguy May 28 '14 at 15:34
3

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).

  • Voting up this answer because it's recent and Moshe is a senior member of the Drupal community. In the accepted answer, the only stable D7 module is CNR, and that's being depreciated in favour of CER (which has no stable release as of yet). – aendrew Jan 15 '13 at 18:21
  • 1
    Relation haven't had lots of issues even back in March. And Moshe Eshel drupal.org/user/168876 != Moshe Weitzman drupal.org/user/23 (look at that uid...) btw. – user49 Jan 26 '13 at 23:56
  • @chx I was surprised myself to see me featured as a senior member... I am trying to be a better contributor but I don't think I'll ever rise to the level of the Original Moshe. Concerning the Relation module, I based that on feedback from someone in the know (I won't name names) - I haven't had the opportunity to really test it at the time, and relied on that opinion. I'm currently working on a D7 project - I think I have the need for it so I'll give my updated opinion here when I'm done. – Moshe Eshel Jan 27 '13 at 8:04
2

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.

1

You can also try the Reverse Node Reference module.

0

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

0

You can use the Content Dependency module.

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

  • 1
    Not really sure how this can be used to create mutual references? – mpdonadio May 14 '13 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.