Hi y'all,

I've looked around quite a bit to find an answer to this issue, but I haven't found a good answer to how to clone a node an arbitrary number of times using Rules, while incrementing a value in each cloned node. I wanted to write in my solution, and ask if there are a few changes to be made/if this can be used for documentation.

Here is my use case: I have a reservation system in which someone may reserve a piece of equipment every morning for four days, but they don't want to make four reservations, and they don't want to block out all of the time because someone else wants to use it afternoon/evening. So, I added a decimal field to my reservation called "Clone this for X days." If someone puts in, say, 3, then the reservation for the current day will be made, along with one reservation each day for 3 days. Along with this, I have a Views Bulk Operations checkbox and "delete own node" rule so that people can delete any accidentally created nodes, or to cancel the nodes later (the rule checks the user is the owner of the selected node).

I have attached the export code to this post, but here's the gist of what I did: First, I also installed Rules Bonus Pack (clones nodes and turns numbers into dates), Batched Rules (if a large number of clones needs to be made), and Conditional Rules (for an advanced loop).

Once the reservation node was found to be valid and has a clone number that's greater than zero, the rule rounds up the clone number to the next integer (for people who put in, say, 1.5), and creates a variable called "copies" which is a decimal with the same value as the number of clones requested. Next, I set up a while loop that converts the reservation dates into numbers, converts the "copies" number from number of days to number of seconds, and adds this number to the converted dates. The dates are then converted back to dates. After this, the copies number was recalculated as copies-1. With the above mentioned numbers, the original number is cloned, and the dates set to the calculated dates.

Overall, what happens is, the first reservation is made, then the last in the series is made, then the rule iterates backwards from the last reservation to the first. The rule stops when "copies" goes to zero.

If you want to set up your own rule to do something similar, a very useful tool is to add a "display a message on the site" in order to see where problems might arise. Another thing you might also want to do is add a rule component that sets the "clone this node X times" number to 0 for all of the nodes, because if you re-save the nodes later, you might clone all of them accidentally.

Again, let me know if this is a helpful start for cloning an arbitrary number of nodes and iterating some value of the nodes (such as day) in each created node. While there were a couple Rules-extending modules involved, Rules has been super useful in my sites!

CommentFileSizeAuthor
rule.txt5.14 KBlaboratory.mike
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TR’s picture

Component: Rules Core » Documentation
Category: Feature request » Task
Issue tags: +Novice, +Contributor

Yes, this is helpful - it would be nice to add this writeup and maybe some screen shots to the Rules documentation at Tutorials & Exports. Is that something you can do, @laboratory.mike?

Or maybe someone else can step up and create this documentation? Should be quite easy to do - just ask here if you need help.

TR’s picture

Version: 7.x-2.3 » 7.x-2.x-dev
Assigned: laboratory.mike » Unassigned