5

I want to have a node reference for a content type. I have two content types A and B. I want to reference content B from a field in content type A. In the same way I want to refer content type A from content type B. What I want is If I referenced content type B from content type A, Ta field should be added automatically to content type B with reference to content A.

Linking from A to B should make it linking from B to A too. Both should have updated fields for this.

How can I do this? Which module can I use for this?

5 Answers 5

4

There has been a lot of movement around this subject lately.

The state of the art module seems to be Entity Reference.

It may lack some functionality you're asking for, so cnr may be of help too.

4
  • this module wont create references bidirectional, so you need create relation using Entity Reference module, then make it bidirectional in a small module Commented Apr 4, 2012 at 6:45
  • I completed my answer with the cnr reference. No code needed, which suits the OP question.
    – Countzero
    Commented Apr 4, 2012 at 6:46
  • cnr has exactly the same functionality I wanted. Thank you so much Commented Apr 4, 2012 at 11:27
  • 2
    There is a new kid on the block, that extends and generalizes Corresponding Node References to the Entity system. If you're reading this now, please test and try out the Corresponding Entity Reference module: drupal.org/project/cer Commented Aug 6, 2012 at 21:25
2

Well, I found a solution that sort of works, the basic functionality works exactly like you describe, my problems with it are more around the edges (but I will put those in a separate question). The only way I found to do this using existing modules and no custom code is using the References Module This is a bit dated module, and hopefully in the future, Entity Reference will have the needed functionality, but it doesn't at the moment...

OK, so after you install the References Module, you need to install the Corresponding node references module and follow the exact instructions on the module page:

  • To install enable the module at admin/build/modules
  • Create node type A
  • Create node type B
  • Create a node reference field on node type A pointing to node B
  • Create a node reference field on node type B pointing to node A
  • Go to the settings page at admin/config/system/corresponding_node_references. Select to enable the corresponding referencing for these node types pointing to each other.
  • Create some nodes and reference them to each other

From that moment on, once you reference it one way, the other way synchronizes by itself (using the newer Entity Reference module you have to do this manually).

Hope this helps.

1

The Relation module gives bidirectional references and more. It creates a node to describe the relationship which can itself can have fields. That lets you do things like describe how/why one node relates to another, for example, was the Person entity a director/actor/taxonomy term in the Movie entity it relates to?

1

Working with "entity reference" module which can be extended with (Entity)Reference Field Synchronization module (http://drupal.org/project/ref_field) to synchronize two entities, fields next article/ previous article for example. When "next article" is set in article A, "previous article" is synchronized in article B. There is currently an error message when installing, but a patch is available here : http://drupal.org/node/1670356 and works just fine.

0

I think you can write your module and you need provide this hooks:

//create relation
function hook_node_insert($node) {
  switch($node->type):
    case 'A':
      update refereed node from type B
    break;
    case 'B':
      update refereed node from type A
    break;
  endswitch;
}
//update relation
hook_node_update()
//delete relation
hook_node_delete()
  1. http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_insert/7
  2. http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_update/7
  3. http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_delete/7
1
  • before this you need create references by other modules like Entity Reference Commented Apr 4, 2012 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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