=== modified file 'includes/form.inc' --- includes/form.inc 2006-08-22 19:12:05 +0000 +++ includes/form.inc 2006-08-23 03:55:39 +0000 @@ -679,8 +679,7 @@ function _element_info($type, $refresh = $cache[$element_type] = array_merge_recursive($basic_defaults, $info); } } - } - + } return $cache[$type]; } === modified file 'modules/book/book.module' --- modules/book/book.module 2006-08-18 18:58:44 +0000 +++ modules/book/book.module 2006-08-23 03:58:15 +0000 @@ -187,18 +187,19 @@ function book_load($node) { return db_fetch_object(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid)); } -/** - * Implementation of hook_insert(). - */ -function book_insert($node) { - db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight); +function book_form_alter($form_id, &$form) { + if ($form_id == 'book_node_form') { + // add own handler for saving ancestry to {book} table + $form['#submit']['book_node_submit'] = array(); + } + elseif ($form_id == 'node_delete_confirm') { + $form['#submit']['book_node_delete_confirm_submit'] = array(); + } } -/** - * Implementation of hook_update(). - */ -function book_update($node) { - if ($node->revision) { +function book_node_submit($form_id, $node) { + $node = (object)$node; + if ($node->is_new || $node->revision) { db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight); } else { @@ -206,31 +207,16 @@ function book_update($node) { } } -/** - * Implementation of hook_delete(). - */ -function book_delete(&$node) { - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); -} - -/** - * Implementation of hook_submit(). - */ -function book_submit(&$node) { - global $user; - // Set default values for non-administrators. - if (!user_access('administer nodes')) { - $node->weight = 0; - $node->revision = 1; - $book->uid = $user->uid; - $book->name = $user->uid ? $user->name : ''; - } +function book_node_delete_confirm_submit($form_id, $form_values) { + db_query('DELETE FROM {book} WHERE nid = %d OR parent = %d', $form_values['node']->nid, $form_values['node']->nid); } /** * Implementation of hook_form(). */ function book_form(&$node) { + global $user; + $type = node_get_types('type', $node); if ($node->nid && !$node->parent && !user_access('create new books')) { $form['parent'] = array('#type' => 'value', '#value' => $node->parent); @@ -265,20 +251,21 @@ function book_form(&$node) { '#weight' => 5, '#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'), ); + + $form['weight'] = array('#type' => 'weight', + '#title' => t('Weight'), + '#access' => user_access('administer nodes'), + '#default_value' => $node->weight, + '#delta' => 15, + '#weight' => 5, + '#description' => t('Pages at a given level are ordered first by weight and then by title.'), + ); - if (user_access('administer nodes')) { - $form['weight'] = array('#type' => 'weight', - '#title' => t('Weight'), - '#default_value' => $node->weight, - '#delta' => 15, - '#weight' => 5, - '#description' => t('Pages at a given level are ordered first by weight and then by title.'), - ); - } - else { + if (!user_access('administer nodes')) { // If a regular user updates a book page, we create a new revision // authored by that user: - $form['revision'] = array('#type' => 'hidden', '#value' => 1); + $form['revision'] = array('#type' => 'value', '#value' => 1); + $form['name'] = array('#type' => 'value', '#value' => $user->uid ? $user->name : ''); } return $form; @@ -479,9 +466,6 @@ function book_nodeapi(&$node, $op, $teas case 'delete revision': db_query('DELETE FROM {book} WHERE vid = %d', $node->vid); break; - case 'delete': - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); - break; } } === modified file 'modules/comment/comment.module' --- modules/comment/comment.module 2006-08-22 11:13:03 +0000 +++ modules/comment/comment.module 2006-08-23 02:59:10 +0000 @@ -280,6 +280,7 @@ function comment_form_alter($form_id, &$ } elseif (isset($form['type'])) { if ($form['type']['#value'] .'_node_form' == $form_id) { + $form['#submit']['comment_node_submit'] = array(); $node = $form['#node']; $form['comment_settings'] = array( '#type' => 'fieldset', @@ -297,6 +298,10 @@ function comment_form_alter($form_id, &$ ); } } + + if ($form_id == 'node_delete_confirm') { + $form['#submit']['comment_node_delete_confirm_submit'] = array(); + } } /** @@ -315,15 +320,6 @@ function comment_nodeapi(&$node, $op, $a } break; - case 'insert': - db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid); - break; - - case 'delete': - db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid); - db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid); - break; - case 'update index': $text = ''; $comments = db_query('SELECT subject, comment, format FROM {comments} WHERE nid = %d AND status = %d', $node->nid, COMMENT_PUBLISHED); @@ -346,6 +342,17 @@ function comment_nodeapi(&$node, $op, $a } } +function comment_node_delete_confirm_submit($form_id, $form_values) { + db_query('DELETE FROM {comments} WHERE nid = %d', $form_values['node']->nid); + db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $form_values['node']->nid); +} + +function comment_node_submit($form_id, $node) { + if ($node->is_new) { + db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid); + } +} + /** * Implementation of hook_user(). * === modified file 'modules/forum/forum.module' --- modules/forum/forum.module 2006-08-20 05:57:40 +0000 +++ modules/forum/forum.module 2006-08-22 21:26:30 +0000 @@ -233,6 +233,39 @@ function forum_form_alter($form_id, &$fo unset($form['nodes']['forum']); } } + + if ($form_id == 'forum_node_form') { + // add own handlers to node form + $form['#validate']['forum_node_validate'] = array(); + // prepend this handler to insure that it executes before node_form_submit() performs its save. + array_unshift($form['#submit'], array('forum_node_submit_early' => array())); + $form['#submit']['forum_node_submit'] = array(); + } + + if ($form_id == 'node_delete_confirm' && $form['node']['#value']->type == 'forum') { + $form['#submit']['forum_node_delete_confirm_submit'] = array(); + } +} + +/** + * Check in particular that only a "leaf" term in the associated taxonomy + * vocabulary is selected, not a "container" term. + */ +function forum_node_validate($form_id, $node) { + $node = (object)$node; + if ($node->taxonomy) { + // Extract the node's proper topic ID. + $vocabulary = variable_get('forum_nav_vocabulary', ''); + $containers = variable_get('forum_containers', array()); + foreach ($node->taxonomy as $term) { + if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) { + if (in_array($term, $containers)) { + $term = taxonomy_get_term($term); + form_set_error('taxonomy', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => theme('placeholder', $term->name)))); + } + } + } + } } /** @@ -327,12 +360,9 @@ function forum_view(&$node, $teaser = FA } /** - * Implementation of hook_submit(). - * - * Check in particular that only a "leaf" term in the associated taxonomy - * vocabulary is selected, not a "container" term. + * An early submit handler of the forum_node_form. Runs before the node is saved. */ -function forum_submit(&$node) { +function forum_node_submit_early($form_id, &$node) { // Make sure all fields are set properly: $node->icon = $node->icon ? $node->icon : ''; @@ -359,32 +389,9 @@ function forum_submit(&$node) { } } -/** - * Implementation of hook_validate(). - * - * Check in particular that only a "leaf" term in the associated taxonomy - * vocabulary is selected, not a "container" term. - */ -function forum_validate($node) { - if ($node->taxonomy) { - // Extract the node's proper topic ID. - $vocabulary = variable_get('forum_nav_vocabulary', ''); - $containers = variable_get('forum_containers', array()); - foreach ($node->taxonomy as $term) { - if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vocabulary))) { - if (in_array($term, $containers)) { - $term = taxonomy_get_term($term); - form_set_error('taxonomy', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name))); - } - } - } - } -} -/** - * Implementation of hook_update(). - */ -function forum_update($node) { +function forum_node_submit($node) { + $node = (object)$node; if ($node->revision) { db_query("INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)", $node->nid, $node->vid, $node->tid); } @@ -424,18 +431,8 @@ function forum_prepare(&$node) { } } -/** - * Implementation of hook_insert(). - */ -function forum_insert($node) { - db_query('INSERT INTO {forum} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $node->tid); -} - -/** - * Implementation of hook_delete(). - */ -function forum_delete(&$node) { - db_query('DELETE FROM {forum} WHERE nid = %d', $node->nid); +function forum_node_delete_confirm_submit($form_id, $form_values) { + db_query('DELETE FROM {forum} WHERE nid = %d', $form_values['node']->nid); } /** === modified file 'modules/menu/menu.module' --- modules/menu/menu.module 2006-08-22 11:13:03 +0000 +++ modules/menu/menu.module 2006-08-23 02:59:10 +0000 @@ -147,31 +147,20 @@ function menu_block($op = 'list', $delta } } -/** - * Implementation of hook_nodeapi(). - */ -function menu_nodeapi(&$node, $op) { - - if (user_access('administer menu')) { - switch ($op) { - case 'insert': - case 'update': - if ($node->menu['delete']) { - menu_node_form_delete($node); - menu_rebuild(); - } - elseif ($node->menu['title']) { - $node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid"; - menu_edit_item_save($node->menu); - menu_rebuild(); - } - break; +function menu_node_delete_confirm_submit($form_id, $form_values) { + menu_node_form_delete($form_values['node']); + menu_rebuild(); +} - case 'delete': - menu_node_form_delete($node); - menu_rebuild(); - break; - } +function menu_node_submit($form_id, $node) { + if ($node->menu['delete']) { + menu_node_form_delete($node); + menu_rebuild(); + } + elseif ($node->menu['title']) { + $node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid"; + menu_edit_item_save($node->menu); + menu_rebuild(); } } @@ -198,6 +187,8 @@ function menu_form_alter($form_id, &$for $item = !is_array($item) ? $edit['menu'] : (($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item)); } } + + $form['#submit']['menu_node_submit'] = array(); $form['menu'] = array('#type' => 'fieldset', '#title' => t('Menu settings'), @@ -259,6 +250,10 @@ function menu_form_alter($form_id, &$for ); } } + + if ($form_id == 'node_delete_confirm') { + $form['#submit']['menu_node_delete_confirm_submit'] = array(); + } } /** === modified file 'modules/node/node.module' --- modules/node/node.module 2006-08-22 11:13:03 +0000 +++ modules/node/node.module 2006-08-23 04:31:24 +0000 @@ -3,7 +3,27 @@ /** * @file - * The core that allows content to be submitted to the site. + * The core that allows content to be submitted to the site. Modules and scripts may + * programmatically submit nodes using the usual form API pattern. + + * CREATE/EDIT NODE EXAMPLE + * $type = 'story'; + * $node = array('type' => $type); + * $form = drupal_retrieve_form($type. '_node_form', $node); + * $form['#post']['edit']['nid'] = 112; // send this if performing an edit + * $form['#post']['edit']['name'] = 'amy'; + * $form['#post']['edit']['title'] = 'robotitle'; + * $form['#post']['edit']['body'] = 'hello world'; + * $goto = drupal_process_form($type. '_node_form', $form); + * + * DELETE SINGLE NODE EXAMPLE + * $node = node_load(112); + * $form = drupal_retrieve_form('node_delete_confirm', $node); + * $form['#post']['op'] = t('Delete'); + * $goto = drupal_process_form('node_delete_confirm', $form); + * + * Calling form_get_errors() will list any validation errors that prevented the + * form from being submitted. */ define('NODE_NEW_LIMIT', time() - 30 * 24 * 60 * 60); @@ -456,9 +476,9 @@ function node_invoke_nodeapi(&$node, $op * @return * A fully-populated node object. */ -function node_load($param = array(), $revision = NULL, $reset = NULL) { +function node_load($param = array(), $revision = NULL, $reset = NULL) { static $nodes = array(); - + if ($reset) { $nodes = array(); } @@ -601,16 +621,6 @@ function node_save(&$node) { db_query($node_query, $node_table_values); db_query($revisions_query, $revisions_table_values); - // Call the node specific callback (if any): - if ($node->is_new) { - node_invoke($node, 'insert'); - node_invoke_nodeapi($node, 'insert'); - } - else { - node_invoke($node, 'update'); - node_invoke_nodeapi($node, 'update'); - } - // Update the node access table for this node. node_access_acquire_grants($node); @@ -1072,6 +1082,8 @@ function node_menu($may_cache) { 'path' => 'node/add/'. $type_url_str, 'title' => t($name), 'access' => node_access('create', $type->type), + 'callback' => 'drupal_get_form', + 'callback arguments' => array($type->type. '_node_form', array('type' => $type->type)) ); } } @@ -1100,7 +1112,7 @@ function node_menu($may_cache) { 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'node/'. arg(1) .'/delete', 'title' => t('delete'), 'callback' => 'drupal_get_form', - 'callback arguments' => array('node_delete_confirm'), + 'callback arguments' => array('node_delete_confirm', $node), 'access' => node_access('delete', $node), 'weight' => 1, 'type' => MENU_CALLBACK); @@ -1757,53 +1769,9 @@ function node_feed($nodes = 0, $channel } /** - * Prepare node for save and allow modules to make changes. + * A form validate handler. Perform checks on the given node. */ -function node_submit($node) { - global $user; - - // Convert the node to an object, if necessary. - $node = (object)$node; - - // Auto-generate the teaser, but only if it hasn't been set (e.g. by a - // module-provided 'teaser' form item). - if (!isset($node->teaser)) { - $node->teaser = isset($node->body) ? node_teaser($node->body, isset($node->format) ? $node->format : NULL) : ''; - } - - $access = user_access('administer nodes'); - if ($access) { - // Populate the "authored by" field. - if ($account = user_load(array('name' => $node->name))) { - $node->uid = $account->uid; - } - else { - $node->uid = 0; - } - - $node->created = $node->date ? strtotime($node->date) : NULL; - } - // Force defaults in case people modify the form: - $node_options = variable_get('node_options_'. $node->type, array('status', 'promote')); - foreach (array('status', 'promote', 'sticky', 'revision') as $key) { - if (!$access || !isset($node->$key)) { - $node->$key = in_array($key, $node_options); - } - } - - // Do node-type-specific validation checks. - node_invoke($node, 'submit'); - node_invoke_nodeapi($node, 'submit'); - - $node->validated = TRUE; - - return $node; -} - -/** - * Perform validation checks on the given node. - */ -function node_validate($node, $form = array()) { +function node_form_validate($form_id, $node) { // Convert the node to an object, if necessary. $node = (object)$node; $type = node_get_types('type', $node); @@ -1832,14 +1800,6 @@ function node_validate($node, $form = ar form_set_error('date', t('You have to specify a valid date.')); } } - - // Do node-type-specific validation checks. - node_invoke($node, 'validate', $form); - node_invoke_nodeapi($node, 'validate', $form); -} - -function node_form_validate($form_id, $form_values, $form) { - node_validate($form_values, $form); } function node_object_prepare(&$node) { @@ -1858,9 +1818,11 @@ function node_object_prepare(&$node) { } /** - * Generate the node editing form array + * Generate the node add/edit form array. */ function node_form($node) { + global $user; + $node = (object)$node; node_object_prepare($node); @@ -1884,54 +1846,31 @@ function node_form($node) { $form['title']['#weight'] = -5; } + // Populate $node so we can assign it to $form['#node]. $node_options = variable_get('node_options_'. $node->type, array('status', 'promote')); // If this is a new node, fill in the default values. if (!isset($node->nid)) { foreach (array('status', 'promote', 'sticky', 'revision') as $key) { $node->$key = in_array($key, $node_options); } - global $user; - $node->uid = $user->uid; } else { // Nodes being edited should always be preset with the default revision setting. $node->revision = in_array('revision', $node_options); } $form['#node'] = $node; - - // Node author information for administrators - $form['author'] = array( - '#type' => 'fieldset', - '#access' => user_access('administer nodes'), - '#title' => t('Authoring information'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#weight' => 20, - ); - $form['author']['name'] = array('#type' => 'textfield', '#title' => t('Authored by'), '#maxlength' => 60, '#autocomplete_path' => 'user/autocomplete', '#default_value' => $node->name ? $node->name : '', '#weight' => -1, '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', 'Anonymous')))); - $form['author']['date'] = array('#type' => 'textfield', '#title' => t('Authored on'), '#maxlength' => 25, '#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => $node->date))); - - if (isset($node->nid)) { - $form['author']['date']['#default_value'] = $node->date; - } - - // Node options for administrators - $form['options'] = array( - '#type' => 'fieldset', - '#access' => user_access('administer nodes'), - '#title' => t('Publishing options'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#weight' => 25, - ); + + // Node author information is editable only by administrators. + $form['author'] = array('#type' => 'fieldset', '#title' => t('Authoring information'), '#access' => user_access('administer nodes'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 20); + $form['author']['name'] = array('#type' => 'textfield', '#title' => t('Authored by'), '#maxlength' => 60, '#autocomplete_path' => 'user/autocomplete', '#default_value' => $node->nid ? $node->name : $user->name, '#weight' => -1, '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', 'Anonymous')))); + $form['author']['date'] = array('#type' => 'textfield', '#title' => t('Authored on'), '#maxlength' => 25, '#default_value' => $node->date, '#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => $node->date))); + + // Node options are editable only by administrators. + $form['options'] = array('#type' => 'fieldset', '#title' => t('Publishing options'), '#access' => user_access('administer nodes'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => 25); $form['options']['status'] = array('#type' => 'checkbox', '#title' => t('Published'), '#default_value' => $node->status); $form['options']['promote'] = array('#type' => 'checkbox', '#title' => t('Promoted to front page'), '#default_value' => $node->promote); $form['options']['sticky'] = array('#type' => 'checkbox', '#title' => t('Sticky at top of lists'), '#default_value' => $node->sticky); $form['options']['revision'] = array('#type' => 'checkbox', '#title' => t('Create new revision'), '#default_value' => $node->revision); - // These values are used when the user has no administrator accesss. - foreach (array('uid', 'created') as $key) { - $form[$key] = array('#type' => 'value', '#value' => $node->$key); - } // Add the buttons. $form['preview'] = array('#type' => 'button', '#value' => t('Preview'), '#weight' => 40); @@ -2003,40 +1942,30 @@ function theme_node_form($form) { } /** - * Present a node submission form or a set of links to such forms. + * Present set of list of node creation links */ -function node_add($type = NULL) { +function node_add() { global $user; $types = node_get_types(); $type = isset($type) ? str_replace('-', '_', $type) : NULL; - // If a node type has been specified, validate its existence. - if (isset($types[$type]) && node_access('create', $type)) { - // Initialize settings: - $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type); + + foreach ($types as $type) { + if (function_exists($type->module .'_form') && node_access('create', $type->type)) { + $type_url_str = str_replace('_', '-', $type->type); + $title = t('Add a new @s.', array('@s' => $type->name)); + $out = '