Community Documentation

Updating a referenced node with a value from the node referencing it

Last updated November 9, 2012. Created by lyricnz on November 9, 2012.
Log in to edit this page.

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

Massive thanks lyricnz

Massive thanks lyricnz

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Level
Beginner
Audience
Site builders
Keywords
node references, Rules

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.