Setting data values Using Rules

Last updated on
30 November 2019

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Setting entity properties, or more commonly, filling in field values, is a very common use.
Values will need to be available, so read Data availability first.

This works on all data types, such as populating fields on entities, setting values in user profiles, entering a value in a form, etc.

Use case:
* Creating a node and filling in field values.
* Set a manually entered value
* Populate an entity reference field
* Change a user account name
* Changing a user's theme

How to update different content types - with entity reference

Scenario
You have two content types (Type_A and Type_B) and they are related by an entity reference field. eg. Type_B has an entity reference field to it's parent node of Type_A.

When Type_B is updated you want Type_A to be updated as well. This can't be solved in a single rule for Type_B, because Type_A has different fields and they are not available via the data selector.

Solution
The trick is to make a Component Rule for updating Type_A and then call the component via an action on the Type_B event.

  1. Make a new Component Rule named "Update referenced entity"
  2. Add a parameter of type Node, machine name "referenced_entity"
  3. Save and continue editing.
  4. Add a new condition that content is of type "Type_A" (this gives access to Type_A's fields)
  5. As an action, set a data value to update a field in Type_A.
  6. Save the component.
  7. Return to your rule for Type_B events.
  8. As an action choose the "Update referenced entity" component you just made.
  9. For the input select the referenced entity field on Type_B.
  10. Save the rule and test.

To make the above example more useful, you might include additional parameters for your component so that you can pass in values from Type_B and have them used on Type_A. Although at the time of writing there appears to be a bug in accessing text input parameters via tokens/data-selector. See http://drupal.org/node/1547160

Help improve this page

Page status: No known problems

You can: