Let's say you're creating a node, and that node type contains a field which is a reference to another node. Whenever a node is created, you want to update the referenced node with some piece of information from the node that was just created. Here's a Rules recipe for that:

  1. create a node type "Referenced Node", with a text field that will ultimately be updated by the rule (called "last mention")
  2. create a node type "Trigger Type", which contains an entity reference field (called "the other one")
  3. create a rule "After saving new content":
    1. add a condition: Content is of type node+triggertype on the [node] (that was just created)
    2. add a condition: Content is of type node+referencednode on the [node:field-the-other-one] (the reference field in the node)
    3. add an action: Set data value, [node:field-the-other-one:field-last-mention] to "He said: [node:title]". This looks through through the reference in the node that was created to the referenced node, and into a field on that node. The reason that Rules knows about the field is because we added the second condition above!

33 AM.png

Tested by:
- create a referenced_node: title=ReferencedNodeA, LastMentioned=Nobody
- created a trigger_node: title=TriggerNodeA, TheOtherOne==>ReferencedNodeA
- confirmed that rule executed (debugging on), and that the referenced node had it's LastMentioned changed to: "He said: TriggerNodeA"

The rule export is attached below.

AttachmentSize
9:11:12 10:33 AM.png128.55 KB
rules-referenced-node-update.txt666 bytes

Comments

giorgio79’s picture

jkirkby’s picture

Hi thanks for this just what i was looking for,

One issue the data being sent back to field-last-mention [node:title] is being displayed as [node:title] not the actual title, any clues to fix this?

sgurlt’s picture

How to do that, if the reference field is a multi value field?

avorio’s picture

Hi @sg88, all! This is exactly what I'm trying to do right now: to handle multi-value field (entity reference). I'm not getting the fields of that referenced entity in the list of fields available to update. How do I do that, please?

Liam_Mc’s picture

I'm also trying to do this, in handle multi-value entity reference fields. Did anyone here find a solution?
Many thanks for the great example.

Fabito’s picture

Hi, same problem with a multi-value entity.
Somebody above finded a solution ?

dzAhmed’s picture

Any solutions?

JakeRogers’s picture

Sadly, this scheme simply does NOT work for me!!! I do wish Drupal.org would invoke some standards to remove such erroneous information from the site. I've wasted countless hours chasing phantoms down rabbit holes such as this one.

Jake