diff options
Diffstat (limited to 'modules/node/node.module')
-rw-r--r-- | modules/node/node.module | 241 |
1 files changed, 82 insertions, 159 deletions
diff --git a/modules/node/node.module b/modules/node/node.module index 8d5ad794c..3364aa3a2 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -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); @@ -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 = array()) { } /** - * Prepare node for save and allow modules to make changes. - */ -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. + * A form validate handler. Perform 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 = array()) { 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,14 +1846,13 @@ 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. @@ -1899,39 +1860,17 @@ function node_form($node) { } $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))); + // 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))); - 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 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); @@ -1940,6 +1879,15 @@ function node_form($node) { $form['delete'] = array('#type' => 'button', '#value' => t('Delete'), '#weight' => 50); } $form['#after_build'] = array('node_form_add_preview'); + $form['#validate'] = array( + 'node_form_validate' => array(), + $node->type .'_node_validate' => array(), + ); + $form['#submit'] = array( + $node->type .'_node_submit_early' => array(), + 'node_form_submit' => array(), + $node->type .'_node_submit' => array(), + ); $form['#base'] = 'node_form'; return $form; } @@ -2003,40 +1951,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); - $output = drupal_get_form($type .'_node_form', $node); - drupal_set_title(t('Submit @name', array('@name' => $types[$type]->name))); - } - else { - // If no (valid) node type has been provided, display a node type overview. - 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 = '<dt>'. l($type->name, "node/add/$type_url_str", array('title' => $title)) .'</dt>'; - $out .= '<dd>'. filter_xss_admin($type->description) .'</dd>'; - $item[$type->type] = $out; - } + 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 = '<dt>'. l($type->name, "node/add/$type_url_str", array('title' => $title)) .'</dt>'; + $out .= '<dd>'. filter_xss_admin($type->description) .'</dd>'; + $item[$type->type] = $out; } + } - if (isset($item)) { - uksort($item, 'strnatcasecmp'); - $output = t('Choose the appropriate item from the list:') .'<dl>'. implode('', $item) .'</dl>'; - } - else { - $output = t('No content types available.'); - } + if (isset($item)) { + uksort($item, 'strnatcasecmp'); + $output = t('Choose the appropriate item from the list:') .'<dl>'. implode('', $item) .'</dl>'; + } + else { + $output = t('No content types available.'); } return $output; @@ -2116,31 +2054,27 @@ function theme_node_log_message($log) { return '<div class="log"><div class="title">'. t('Log') .':</div>'. $log .'</div>'; } -function node_form_submit($form_id, $edit) { +function node_form_submit($form_id, &$node) { global $user; - // Fix up the node when required: - $node = node_submit($edit); - - // Prepare the node's body: - if ($node->nid) { - // Check whether the current user has the proper access rights to - // perform this operation: - if (node_access('update', $node)) { - node_save($node); - watchdog('content', t('@type: updated %title.', array('@type' => t($node->type), '%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid)); - drupal_set_message(t('The %post was updated.', array ('%post' => node_get_types('name', $node)))); - } - } - else { - // Check whether the current user has the proper access rights to - // perform this operation: - if (node_access('create', $node)) { - node_save($node); - watchdog('content', t('@type: added %title.', array('@type' => t($node->type), '%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); - drupal_set_message(t('Your %post was created.', array ('%post' => node_get_types('name', $node)))); - } + $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) : ''; } + + // Populate the uid based on name field. + $account = user_load(array('name' => $node->name)); + $node->uid = $account->uid; + $node->created = strtotime($node->date); + + $action = $node->nid ? 'updated' : 'created'; + node_save($node); + watchdog('content', t("@type: $action %title.", array('@type' => t($node->type), '%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid)); + drupal_set_message(t("The %post was $action.", array ('%post' => node_get_types('name', $node)))); + + // redirect the submitter as needed if ($node->nid) { if (node_access('view', $node)) { return 'node/'. $node->nid; @@ -2156,13 +2090,9 @@ function node_form_submit($form_id, $edit) { /** * Menu callback -- ask for confirmation of node deletion */ -function node_delete_confirm() { - $edit = $_POST; - $edit['nid'] = $edit['nid'] ? $edit['nid'] : arg(1); - $node = node_load($edit['nid']); - +function node_delete_confirm($node) { if (node_access('delete', $node)) { - $form['nid'] = array('#type' => 'value', '#value' => $node->nid); + $form['node'] = array('#type' => 'value', '#value' => $node); $output = confirm_form($form, t('Are you sure you want to delete %title?', array('%title' => $node->title)), $_GET['destination'] ? $_GET['destination'] : 'node/'. $node->nid, t('This action cannot be undone.'), @@ -2176,10 +2106,7 @@ function node_delete_confirm() { * Execute node deletion */ function node_delete_confirm_submit($form_id, $form_values) { - if ($form_values['confirm']) { - node_delete($form_values['nid']); - } - + node_delete($form_values['node']->nid); return ''; } @@ -2194,10 +2121,6 @@ function node_delete($nid) { db_query('DELETE FROM {node} WHERE nid = %d', $node->nid); db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid); - // Call the node-specific callback (if any): - node_invoke($node, 'delete'); - node_invoke_nodeapi($node, 'delete'); - // Clear the cache so an anonymous poster can see the node being deleted. cache_clear_all(); |