Dear all,
1. Rules work this task very good:
- Node Type A and node type B have reference field together
- Create node a of Node Type A, will auto-create node b of Node Type B
- Reference node a to node b and corresponding reference node b to node a

2. But How to do the same task, like this:
- Node Type A and node type B have reference field together
- Node a of Node Type A and node b of Node Type B are existing
- Edit node a, reference to node b ---> How to auto-edit node b reference to node a

Comments

drvdt’s picture

Status: Active » Fixed

Dear all,
I have found a simple solution. Only need rules module. Create a rule:

1. Event:
- After saving new content
- After updating existing content
(one or both up to you)
2. Conditions
- Content is of type: Chose Content: [node], Content types: Node Type A
- Content is of type: Content:[node:field-reference_to_node_type_b], Content types: Node Type B
3. Actions
- Add an item to a list
Parameter: List:[node:field-refrence_to_node_type_b:field-reference_to_node_type_a], item to add:[node], enforce uniqueness: true

There is all
More information, please see http://drupal.org/node/1836240

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

razkovnik’s picture

It works perfectly when the field-reference_to_node_type_b is limited to 1 value and respectively field-reference_to_node_type_a has unlimited values options.
However - great solution!

bennos’s picture

yeah this works. What I need is sync between ref field on content type a with unlimited values and ref field with unlimited values on content type b.

Did not found a soulution yet for this.

mabho’s picture

This is what I have did for a many-to-many relation in Entity reference. It is kind of complicated, but it works:

*** First: after creating new content of a specific content type ***

1. Event:
- After saving new content
2. Conditions
- Content is of type: Chose Content: [node], Content types: Node Type A
3. Actions
- Add a Loop. I will loop through all the elements of node:field-refrence_to_node_type_b. I am naming the resulting node as $list_item (this is the default name).
- Add a PHP Filter action under the loop - I am doing it with PHP because my entity reference is using views and it looks like Rules cannot identify its content type.

$list_item->field-refrence_to_node_type_b[LANGUAGE_NONE][] = array('target_id' => $node->nid);
node_save($list_item);

*** Second: After updating existing content of a specific content type ***

1. Event:
- After updating existing content
2. Conditions
- Content is of type: Chose Content: [node], Content types: Node Type A
3. Actions
- Add a Loop. I will loop through all the elements of the unpublished node like this: node-unpublished:field-refrence_to_node_type_b. I am naming the resulting node as $list_item (this is the default name).
- Add a PHP Filter action under the loop - I am going to delete all the old references before the node was updated. This way I don`t have to do a lot of checking to see which elements are new and which elements were removed. I will first remove all the old references.

if(isset($list_item->field-refrence_to_node_type_b[LANGUAGE_NONE])) {
  $nid_found = array_search(array('target_id' => $node->nid), $list_item->field-refrence_to_node_type_b[LANGUAGE_NONE], TRUE);
  if($nid_found !== FALSE) {
    unset($list_item->field-refrence_to_node_type_b[LANGUAGE_NONE][$nid_found]);
    // array_values($list_item->field-refrence_to_node_type_b[LANGUAGE_NONE]);
    node_save($list_item);
  }
}

- Add a Loop again. I will loop through all the elements of the published node like this: node:field-refrence_to_node_type_b. I am naming the resulting node as $list_item (this is the default name).
- Add a PHP Filter action under the loop - I am going to add all references back.

$list_item->refrence_to_node_type_b[LANGUAGE_NONE][] = array('target_id' => $node->nid);
node_save($list_item);

======

Now you would have to apply more or less the same logic to node_type_b. When editing node_type_b the same rules should apply and it should update all the references in node_type_a.