Attached is an example of how to use Rules to automatically create and refer to a node when content of selected types are posted. In the example, Story nodes reference Page nodes, and when a Story is submitted a Page is automatically created and referenced from the Story (using a standard CCK node reference field).

The tricky part in this Rule is to save the new node, so that it can be referenced. To do this before the referring takes place, you can create separate rules for the node creation and the node reference and bundle them into a rule set. The option force immediate saving can also be used to avoid postponing the save until all rules have finished. The rule set itself is called by a standard triggered rule.

Note that the id for the referenced node is inserted via a very short PHP snippet.

The example is available to download as a rules export, or to install as a feature. The latter also includes the two node types. To make it work with the export script you'll have to create a node reference field named field_reference in the story node manually.

Good luck!

Johan Falk, NodeOne, Sweden

AttachmentSize
create_and_reference.tgz2.57 KB
create and reference.txt7.95 KB

Comments

maxplus’s picture

Hi,
this was the thing I am looking for.

It would be great if I can change this a little bit to not only create one single page node but the creation of multiple page nodes at once, depending on a value (number) of a cck field.
They should all reference to the same parent Story node...

As a test I already changed your Rule a little bit and instead of just calling the rule set once, I use a the function "execute custom php-code" and used this php loop to call the rule set five times:

for ($i = 5 ; $i > 0; $i--) {
rules_invoke_rule_set('rules_create_and_reference', $node);
}

The problem is that the referencing happens in the wrong direction...

So I actually need the Reference to be made in the other direction, meaning that the newly created pages should be referenced to the single Story and not the single Story referenced to a single page...