Fix for need help with node save

  1. /**
  2. * problem faced is that when it enters presave condition of the nodeapi
  3. * it enters function example_split and when it reaches the nodesave
  4. * condition it goes into an infinite loop creating garbage nodes
  5. * on further debugging I noticed that the node_save condition itself calls the
  6. * the presave condition at the very beginning and which puts it into an infinite loop
  7. *
  8. * when I try with the insert condition the code works perfectly fine but
  9. * the original node is not getting modified and it still holds its content
  10. */
  11.  
  12. function example_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  13.   if ($node->type == 'book' && $node->html2book && user_access('use html2book')) {
  14.     switch ($op) {
  15.     case 'presave':
  16.       example_split($node);
  17.       break;
  18.     }
  19.   }
  20. }
  21.  
  22. function example_split(&$node) {
  23. //some code here to break the body into
  24. // multiple bodies and title and do node save
  25. //of the original node with a new body and title
  26. //as well as create some new nodes  with the
  27. // other body's and titles
  28. if (empty($node->nid)) {
  29.             node_save($node);
  30.      }
  31. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.