I have some business logic in my application where i need to create 10 extra nodes of a content type, when a node of a specific content type is created from Drupal Content Creation page. I know how to programmatically create a node, and I can achieve it via menu hooks, but cant get them done via hook_node_insert() which takes my application into some sort of a loop(maybe because of node_save()). My menu code is an under but I want it to be run from node insert hook.

function nodecreator_menu() {
    $items['admin/create/nodes'] = array(
      'page callback' => 'nc',
     'access arguments' => array('access content'),

    return $items;
function nc(){
      $node2 = new stdClass(); // We create a new node object
  $node2->language = LANGUAGE_NONE;
  $node2->field_vote_year[$node2->language][0]['value']="2009-01-01 00:00:00";
  $node2->type = "vote"; // Or any other content type you want
   $node2->title = $node->title.$node2->field_vote_year[$node2->language][0]['value'];

  node_object_prepare($node2); // Set some default values.
  $node2->uid = 1; // Or any id you wish
  $node2 = node_submit($node2); // Prepare node for a submit

  • Why don't you use form hooks? If you will create nodes on form submit, it will not loop as user will submit only once, and nothing you do later emulates submit.
    – Mołot
    Dec 18, 2013 at 8:47
  • @Mołot .. because i need to use $node->nid of the original node being inserted as target_id of the nodes i will be creating, so somehow i am going to use nid, will that be possible from form hooks?because $Node is not being passed into form hooks as parameter Dec 18, 2013 at 14:25
  • OK, that's a pretty solid reason not to use form hooks to do it :(
    – Mołot
    Dec 18, 2013 at 14:26
  • @Molot..yeah...one question, can we use menu hooks programatically?i.e not visiting the menu item physically in a browser,calling it from application?something like drupal_http_request?that way i may achieve what i am attending Dec 18, 2013 at 14:30
  • Sure, module_invoke_all() is just for it. If it's a hook, you can call it.
    – Mołot
    Dec 18, 2013 at 14:34

1 Answer 1


You might experience an infinite loop here since every time you save the node (auto-generated or not), hook_node_insert() gets invoked. To prevent this infinite loop, you can use an extra pseudo-property in your hook_node_insert() to dictate if whether or not nodes should be automatically created.

Below is an example of how your hook_node_insert() might look like:

// If the node is an auto-generated node, don't create additional nodes.
if (!isset($node->auto_generated)) {
  // Auto-generate 10 nodes.
  for ($i = 0; $i < 10; $i++) {
    // Create a new node object and populate its fields.
    $node2 = new stdClass();
    $node2->language = LANGUAGE_NONE;
    $node2->type = "test";
    $node2->title = "Auto-generated (" . $node->nid . "-" . $i .")";

    $node2->created = REQUEST_TIME;
    $node2->uid = $node->uid;

    // A flag to let hook_nodeapi() implementations know that this is an automatically
    // generated node.
    $node2->auto_generated = TRUE;

    // Finally, save the node.

    watchdog('test', "Auto-created node: <pre>@node</pre>", array(
      '@node' => print_r($node2, TRUE),
  • Thanks for your reply...i tried it,but to no avail, do i have to use node_save with $node->auto_generated=TRUE;? CAN u please further elaborate or paste complete code of hook_node_insert? Dec 18, 2013 at 13:42
  • I got the original code I posted working. For completeness, I added in more detail and comments.
    – angheloko
    Dec 19, 2013 at 3:17
  • Thanks a Lot for your help...This code really saved me from some real jinx...I am really thankful to you..One thing please, how did you know about this pseudo variable?because i couldnt even find it in any sort of documentation on drupal.org Jan 4, 2014 at 8:04
  • It's not in any documentation because it's just a common programming technique. It's not exclusive to Drupal. Nodes, or entities, are just objects which you can attach any additional property to during script execution. In this case, the data you attached to the node helps you with the program's flow. In some cases, you will attach info or metadata that you want to store in the DB, which you will then have to add the necessary codes that will do the saving. :-)
    – angheloko
    Jan 6, 2014 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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