diff -r -u node_template_CVS/node_template.module node_template_CVS_patched/node_template.module --- node_template_CVS/node_template.module 2007-01-26 14:50:24.000000000 +0100 +++ node_template_CVS_patched/node_template.module 2007-01-27 22:34:42.000000000 +0100 @@ -24,13 +24,13 @@ * node_template_load(). Returns node ready to be saved as "new", or cloned or whatever :). */ function node_template_load($nid) { - global $node_templates; + global $node_templates; if (!is_array($node_templates)) { $node_templates = array(); } if (isset($node_templates[$nid])) { - return $node_templates[$nid]; + return drupal_clone($node_templates[$nid]); } // Load template data. If it doesn't exist, return @@ -61,6 +61,7 @@ // TODO: Cleanup node. This part can be removed in future, if modules will implement nodeapi 'template' operation unset($node_templates[$nid]->nid); // make it "new" node :) + unset($node_templates[$nid]->vid); // make it "new" node :) unset($node_templates[$nid]->path); // avoid path alias conflicts! unset($node_templates[$nid]->parent_node); // avoid messing up relativity module's data :) unset($node_templates[$nid]->uid); // do not copy ownership of node :) @@ -73,10 +74,10 @@ * node_template_save(). Returns errors if node doesn't validate, or NULL if all is ok. */ /** - * Save the + * Save node * * @param object $node - * @return array of errrors, or TRUE is all is ok + * @return array of errrors, or TRUE if all is ok */ function node_template_save(&$node) { // avoid "content modified by another user" error in case when item is updated @@ -94,7 +95,18 @@ if ($node->node_template && $node->node_template->php_save) { // Run php code for template. Make node "local" for that code. - drupal_eval('node_template->nid.'];?>'.$node->node_template->php_save); + $GLOBALS['node_template_current'] = &$node; // declare it global this way, otherwise ("global $var; $var =&$node") it doesn't work! + drupal_eval(''.$node->node_template->php_save); + } + + // Check if php_save code did set some errors to prevent saving new node + if (isset($node->node_template->errors) && count($node->node_template->errors) > 0) { + // Convert errors array to format of form_errors array + $errors = array(); + foreach ($node->node_template->errors as $error) { + $errors[] = array('php_save', $error); + } + return $errors; } node_object_prepare($node); @@ -104,11 +116,11 @@ if (!($errors = form_get_errors())) { $node = node_submit($node); node_save($node); + return TRUE; } else { return $errors; } - return TRUE; } /** @@ -244,6 +256,13 @@ '#value' => $node->nid ); + $dbg = print_r($node, true); + $form['tpl_node'] = array( + '#type' => 'item', + '#title' => t('Debug'), + '#value' => '
'.$dbg.'' + ); + $form['title'] = array ( '#type' => 'textfield', '#title' => t('Title'), @@ -261,12 +280,22 @@ '#description' => t('This PHP code will be run after original node is loaded, before template is created. It will be run only once, not every time template node is prepared. Variable $node is full copy of original node, which You can change before it will be cloned to create new nodes. Changes You make with PHP code here will not affect original node.') ); + $tmp = node_template_load($node->nid); + if (!empty($tmp)) { + $dbg = print_r($node, true); + $form['tpl_node_debug_loaded'] = array( + '#type' => 'item', + '#title' => t('Debug'), + '#value' => '
'.$dbg.'' + ); + } + $form['php_save'] = array ( '#type' => 'textarea', '#title' => t('Saving PHP code'), '#default_value' => $template->php_save, '#rows' => 20, - '#description' => t('This PHP code will be run at saving time. It will be run each time new node is created from template, when that new node is saved. Variable $node is full copy of original node, which You can change before it will be cloned to create new nodes.') + '#description' => t('This PHP code will be run at saving time. It will be run each time new node is created from template, when that new node is saved. Variable $node is the node which will be saved along with all Your changes. You can prevent saving this node by adding string to $node->node_template->errors array.') );