1

This is what I have now: Content type 123 has a Entity Reference field referencing content type XYZ. If new content is created of type XYZ then Rules creates new content of type 123. So by creating 1 new node (of type XYZ) I get one extra of type 123.

This is what I'm missing: How do I set the Entity Reference field of 123 correctly by using Rules?

1

In the Rules action you need to use the Set a data value action. This can be used to enter data in fields and properties. Make sure to set the value of your new 123-entity to refer to the xyz entity.

{ "rules_create_new_basic_page_after_saving_article" : {
    "LABEL" : "Create new basic page after saving article",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert--article" : { "bundle" : "article" } },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "page",
            "param_title" : "Basic page to go with [node:title]",
            "param_author" : [ "node:author" ]
          },
          "PROVIDE" : { "entity_created" : { "page_created" : "Created basic page" } }
        }
      },
      { "data_set" : {
          "data" : [ "page-created:field-reference-to-other-entity-" ],
          "value" : [ "node" ]
        }
      },
      { "entity_save" : { "data" : [ "page-created" ], "immediate" : "1" } }
    ]
  }
}
  • Would you do this in a single rule? If not, which Rules Event would you use for the additional rule? Also, which value, or token, would you use to set the value? – Pierre.Vriens Jun 21 '16 at 9:29
  • Would you do this in a single rule? Personal preference, I probably would. Which value, or token, would you use to set the value The value of the xyz entity. The rules data selector is pretty powerful and it will guide you in selecting the right property. – Neograph734 Jun 21 '16 at 9:31
  • @Pierre.Vriens your second question was already in the original answer Make sure to set the value of your new 123-entity to refer to the xyz entity. – Neograph734 Jun 21 '16 at 9:31
  • 1
    Thanks for the updated answer, interesting! Even without any rules condition needed (as in your original answer). I look forward to OPers feedback about it also. PS: and it answers my original comment. So +1 – Pierre.Vriens Jun 21 '16 at 10:01
  • 1
    Thanks Neograph. This is exactly the solution I was looking for. Give me some time to rewrite it to fit my custom values. – Gergo P Jun 21 '16 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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