0

I have a situation where I need to create node(s) in hook_node_insert for certain dates.

Details: There is a content type with date field (start and end-date enabled). The requirement is to create the node with same contents for those many dates.

For example, if the date is from Mar5, 2014 to Mar8, 2014. I need to create node with Startdate & Enddate: Mar5 for one node and Startdate & Enddate: Mar6 for the next node and so on till Mar8 is assigned as start & enddate for the last node.

I got stuck with infinite loop. How do I solve this?

4
  • 2
    I think you may have a design bug - hook_node_insert is for tasks that needs to be executed always. You could, for example, alter node add form's submit handler to foreach on date ranges, or on the opposite end of the spectrum of possibilities, create a cron job to fill missing dates. Or use any other way without abusing hook mentioned. - I'm not saying what you ask is impossible, but I don't think it should ever be really needed.
    – Mołot
    Mar 5 '14 at 8:59
  • For my scenario it is really necessary and needs to be handled. I have events and the criteria is event cannot exceed a day. Although, if the user wanted to add the same events for many consequent date, the application should allow to do so.
    – pown
    Mar 5 '14 at 9:08
  • 3
    but you didn't described how do you want to distinguish nodes that should spawn clones from the ones that should not. Also, I never needed multiple nodes for many-day content, usually everything could be handled using from and to date fields. The more I read, the more convinced I am that you set your mind on a way of solving your requirements before you found this critical flaw, and now you're too deep into development to go back and redesign to another way.
    – Mołot
    Mar 5 '14 at 9:14
  • @Mołot: If you cant give answer to my question, its ok. Please don't twist my question.
    – pown
    Mar 5 '14 at 10:03
2

Why not add a second submit handler using hook_form_alter to the node add form and put your functionality in there as opposed to using hook_node_insert?

That way the code will run everytime the node add form is submitted, but you won't get into the endless loop scenario...

1
  • Excellent.. Works like charm :)
    – pown
    Mar 6 '14 at 14:42
1

You can solve your infinite loop issue with one simple condition...

In hook_node_insert before you are creating new nodes check if start date and end date are not equal.. Because new nodes you are creating carry same start and end dates... So that you don't run into infinite loop situation.. If yes then only run a loop for N + 1 times where N = different of start date and end date and create N + 1 nodes.. That's it :-)

1
  • 1
    Excellent idea. I'll try that.
    – pown
    Mar 5 '14 at 10:02

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.