node.pages.inc

  1. drupal
    1. 6 modules/node/node.pages.inc
    2. 7 modules/node/node.pages.inc
    3. 8 core/modules/node/node.pages.inc

Page callbacks for adding, editing, deleting, and revisions management for content.

Functions & methods

NameDescription
node_addPresent a node submission form or a set of links to such forms.
node_add_page
node_body_fieldReturn a node body field, with format and teaser.
node_delete_confirmMenu callback -- ask for confirmation of node deletion
node_delete_confirm_submitExecute node deletion
node_formGenerate the node add/edit form array.
node_form_build_preview
node_form_delete_submitButton sumit function: handle the 'Delete' button on the node form.
node_form_submit
node_form_submit_build_nodeBuild a node by processing submitted form values and prepare for a form rebuild.
node_form_validate
node_object_prepare
node_page_editMenu callback; presents the node editing form, or redirects to delete confirmation.
node_previewGenerate a node preview.
node_revision_delete_confirm
node_revision_delete_confirm_submit
node_revision_overviewGenerate an overview table of older revisions of a node.
node_revision_revert_confirmAsk for confirmation of the reversion to prevent against CSRF attacks.
node_revision_revert_confirm_submit
theme_node_add_listDisplay the list of available node types for node creation.
theme_node_formPresent a node submission form.
theme_node_previewDisplay a node preview for display during node creation and editing.

File

modules/node/node.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for adding, editing, deleting, and revisions management for content.
  5. */
  6. /**
  7. * Menu callback; presents the node editing form, or redirects to delete confirmation.
  8. */
  9. function node_page_edit($node) {
  10. drupal_set_title(check_plain($node->title));
  11. return drupal_get_form($node->type .'_node_form', $node);
  12. }
  13. function node_add_page() {
  14. $item = menu_get_item();
  15. $content = system_admin_menu_block($item);
  16. return theme('node_add_list', $content);
  17. }
  18. /**
  19. * Display the list of available node types for node creation.
  20. *
  21. * @ingroup themeable
  22. */
  23. function theme_node_add_list($content) {
  24. $output = '';
  25. if ($content) {
  26. $output = '<dl class="node-type-list">';
  27. foreach ($content as $item) {
  28. $output .= '<dt>'. l($item['title'], $item['href'], $item['localized_options']) .'</dt>';
  29. $output .= '<dd>'. filter_xss_admin($item['description']) .'</dd>';
  30. }
  31. $output .= '</dl>';
  32. }
  33. return $output;
  34. }
  35. /**
  36. * Present a node submission form or a set of links to such forms.
  37. */
  38. function node_add($type) {
  39. global $user;
  40. $types = node_get_types();
  41. $type = isset($type) ? str_replace('-', '_', $type) : NULL;
  42. // If a node type has been specified, validate its existence.
  43. if (isset($types[$type]) && node_access('create', $type)) {
  44. // Initialize settings:
  45. $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => '');
  46. drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)));
  47. $output = drupal_get_form($type .'_node_form', $node);
  48. }
  49. return $output;
  50. }
  51. function node_form_validate($form, &$form_state) {
  52. node_validate($form_state['values'], $form);
  53. }
  54. function node_object_prepare(&$node) {
  55. // Set up default values, if required.
  56. $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
  57. // If this is a new node, fill in the default values.
  58. if (!isset($node->nid)) {
  59. foreach (array('status', 'promote', 'sticky') as $key) {
  60. $node->$key = in_array($key, $node_options);
  61. }
  62. global $user;
  63. $node->uid = $user->uid;
  64. $node->created = time();
  65. }
  66. else {
  67. $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
  68. // Remove the log message from the original node object.
  69. $node->log = NULL;
  70. }
  71. // Always use the default revision setting.
  72. $node->revision = in_array('revision', $node_options);
  73. node_invoke($node, 'prepare');
  74. node_invoke_nodeapi($node, 'prepare');
  75. }
  76. /**
  77. * Generate the node add/edit form array.
  78. */
  79. function node_form(&$form_state, $node) {
  80. global $user;
  81. if (isset($form_state['node'])) {
  82. $node = $form_state['node'] + (array)$node;
  83. }
  84. if (isset($form_state['node_preview'])) {
  85. $form['#prefix'] = $form_state['node_preview'];
  86. }
  87. $node = (object)$node;
  88. foreach (array('body', 'title', 'format') as $key) {
  89. if (!isset($node->$key)) {
  90. $node->$key = NULL;
  91. }
  92. }
  93. if (!isset($form_state['node_preview'])) {
  94. node_object_prepare($node);
  95. }
  96. else {
  97. $node->build_mode = NODE_BUILD_PREVIEW;
  98. }
  99. // Set the id of the top-level form tag
  100. $form['#id'] = 'node-form';
  101. // Basic node information.
  102. // These elements are just values so they are not even sent to the client.
  103. foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as $key) {
  104. $form[$key] = array(
  105. '#type' => 'value',
  106. '#value' => isset($node->$key) ? $node->$key : NULL,
  107. );
  108. }
  109. // Changed must be sent to the client, for later overwrite error checking.
  110. $form['changed'] = array(
  111. '#type' => 'hidden',
  112. '#default_value' => isset($node->changed) ? $node->changed : NULL,
  113. );
  114. // Get the node-specific bits.
  115. if ($extra = node_invoke($node, 'form', $form_state)) {
  116. $form = array_merge_recursive($form, $extra);
  117. }
  118. if (!isset($form['title']['#weight'])) {
  119. $form['title']['#weight'] = -5;
  120. }
  121. $form['#node'] = $node;
  122. // Add a log field if the "Create new revision" option is checked, or if the
  123. // current user has the ability to check that option.
  124. if (!empty($node->revision) || user_access('administer nodes')) {
  125. $form['revision_information'] = array(
  126. '#type' => 'fieldset',
  127. '#title' => t('Revision information'),
  128. '#collapsible' => TRUE,
  129. // Collapsed by default when "Create new revision" is unchecked
  130. '#collapsed' => !$node->revision,
  131. '#weight' => 20,
  132. );
  133. $form['revision_information']['revision'] = array(
  134. '#access' => user_access('administer nodes'),
  135. '#type' => 'checkbox',
  136. '#title' => t('Create new revision'),
  137. '#default_value' => $node->revision,
  138. );
  139. $form['revision_information']['log'] = array(
  140. '#type' => 'textarea',
  141. '#title' => t('Log message'),
  142. '#default_value' => (isset($node->log) ? $node->log : ''),
  143. '#rows' => 2,
  144. '#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
  145. );
  146. }
  147. // Node author information for administrators
  148. $form['author'] = array(
  149. '#type' => 'fieldset',
  150. '#access' => user_access('administer nodes'),
  151. '#title' => t('Authoring information'),
  152. '#collapsible' => TRUE,
  153. '#collapsed' => TRUE,
  154. '#weight' => 20,
  155. );
  156. $form['author']['name'] = array(
  157. '#type' => 'textfield',
  158. '#title' => t('Authored by'),
  159. '#maxlength' => 60,
  160. '#autocomplete_path' => 'user/autocomplete',
  161. '#default_value' => $node->name ? $node->name : '',
  162. '#weight' => -1,
  163. '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  164. );
  165. $form['author']['date'] = array(
  166. '#type' => 'textfield',
  167. '#title' => t('Authored on'),
  168. '#maxlength' => 25,
  169. '#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => !empty($node->date) ? $node->date : format_date($node->created, 'custom', 'Y-m-d H:i:s O'))),
  170. );
  171. if (isset($node->date)) {
  172. $form['author']['date']['#default_value'] = $node->date;
  173. }
  174. // Node options for administrators
  175. $form['options'] = array(
  176. '#type' => 'fieldset',
  177. '#access' => user_access('administer nodes'),
  178. '#title' => t('Publishing options'),
  179. '#collapsible' => TRUE,
  180. '#collapsed' => TRUE,
  181. '#weight' => 25,
  182. );
  183. $form['options']['status'] = array(
  184. '#type' => 'checkbox',
  185. '#title' => t('Published'),
  186. '#default_value' => $node->status,
  187. );
  188. $form['options']['promote'] = array(
  189. '#type' => 'checkbox',
  190. '#title' => t('Promoted to front page'),
  191. '#default_value' => $node->promote,
  192. );
  193. $form['options']['sticky'] = array(
  194. '#type' => 'checkbox',
  195. '#title' => t('Sticky at top of lists'),
  196. '#default_value' => $node->sticky,
  197. );
  198. // These values are used when the user has no administrator access.
  199. foreach (array('uid', 'created') as $key) {
  200. $form[$key] = array(
  201. '#type' => 'value',
  202. '#value' => $node->$key,
  203. );
  204. }
  205. // Add the buttons.
  206. $form['buttons'] = array();
  207. $form['buttons']['submit'] = array(
  208. '#type' => 'submit',
  209. '#access' => !variable_get('node_preview', 0) || (!form_get_errors() && isset($form_state['node_preview'])),
  210. '#value' => t('Save'),
  211. '#weight' => 5,
  212. '#submit' => array('node_form_submit'),
  213. );
  214. $form['buttons']['preview'] = array(
  215. '#type' => 'submit',
  216. '#value' => t('Preview'),
  217. '#weight' => 10,
  218. '#submit' => array('node_form_build_preview'),
  219. );
  220. if (!empty($node->nid) && node_access('delete', $node)) {
  221. $form['buttons']['delete'] = array(
  222. '#type' => 'submit',
  223. '#value' => t('Delete'),
  224. '#weight' => 15,
  225. '#submit' => array('node_form_delete_submit'),
  226. );
  227. }
  228. $form['#validate'][] = 'node_form_validate';
  229. $form['#theme'] = array($node->type .'_node_form', 'node_form');
  230. return $form;
  231. }
  232. /**
  233. * Return a node body field, with format and teaser.
  234. */
  235. function node_body_field(&$node, $label, $word_count) {
  236. // Check if we need to restore the teaser at the beginning of the body.
  237. $include = !isset($node->teaser) || ($node->teaser == substr($node->body, 0, strlen($node->teaser)));
  238. $form = array(
  239. '#after_build' => array('node_teaser_js', 'node_teaser_include_verify'));
  240. $form['#prefix'] = '<div class="body-field-wrapper">';
  241. $form['#suffix'] = '</div>';
  242. $form['teaser_js'] = array(
  243. '#type' => 'textarea',
  244. '#rows' => 10,
  245. '#teaser' => 'edit-body',
  246. '#teaser_checkbox' => 'edit-teaser-include',
  247. '#disabled' => TRUE,
  248. );
  249. $form['teaser_include'] = array(
  250. '#type' => 'checkbox',
  251. '#title' => t('Show summary in full view'),
  252. '#default_value' => $include,
  253. '#prefix' => '<div class="teaser-checkbox">',
  254. '#suffix' => '</div>',
  255. );
  256. $form['body'] = array(
  257. '#type' => 'textarea',
  258. '#title' => check_plain($label),
  259. '#default_value' => $include ? $node->body : ($node->teaser . $node->body),
  260. '#rows' => 20,
  261. '#required' => ($word_count > 0),
  262. );
  263. $form['format'] = filter_form($node->format);
  264. return $form;
  265. }
  266. /**
  267. * Button sumit function: handle the 'Delete' button on the node form.
  268. */
  269. function node_form_delete_submit($form, &$form_state) {
  270. $destination = '';
  271. if (isset($_REQUEST['destination'])) {
  272. $destination = drupal_get_destination();
  273. unset($_REQUEST['destination']);
  274. }
  275. $node = $form['#node'];
  276. $form_state['redirect'] = array('node/'. $node->nid .'/delete', $destination);
  277. }
  278. function node_form_build_preview($form, &$form_state) {
  279. $node = node_form_submit_build_node($form, $form_state);
  280. $form_state['node_preview'] = node_preview($node);
  281. }
  282. /**
  283. * Present a node submission form.
  284. *
  285. * @ingroup themeable
  286. */
  287. function theme_node_form($form) {
  288. $output = "\n<div class=\"node-form\">\n";
  289. // Admin form fields and submit buttons must be rendered first, because
  290. // they need to go to the bottom of the form, and so should not be part of
  291. // the catch-all call to drupal_render().
  292. $admin = '';
  293. if (isset($form['author'])) {
  294. $admin .= " <div class=\"authored\">\n";
  295. $admin .= drupal_render($form['author']);
  296. $admin .= " </div>\n";
  297. }
  298. if (isset($form['options'])) {
  299. $admin .= " <div class=\"options\">\n";
  300. $admin .= drupal_render($form['options']);
  301. $admin .= " </div>\n";
  302. }
  303. $buttons = drupal_render($form['buttons']);
  304. // Everything else gets rendered here, and is displayed before the admin form
  305. // field and the submit buttons.
  306. $output .= " <div class=\"standard\">\n";
  307. $output .= drupal_render($form);
  308. $output .= " </div>\n";
  309. if (!empty($admin)) {
  310. $output .= " <div class=\"admin\">\n";
  311. $output .= $admin;
  312. $output .= " </div>\n";
  313. }
  314. $output .= $buttons;
  315. $output .= "</div>\n";
  316. return $output;
  317. }
  318. /**
  319. * Generate a node preview.
  320. */
  321. function node_preview($node) {
  322. if (node_access('create', $node) || node_access('update', $node)) {
  323. // Load the user's name when needed.
  324. if (isset($node->name)) {
  325. // The use of isset() is mandatory in the context of user IDs, because
  326. // user ID 0 denotes the anonymous user.
  327. if ($user = user_load(array('name' => $node->name))) {
  328. $node->uid = $user->uid;
  329. $node->picture = $user->picture;
  330. }
  331. else {
  332. $node->uid = 0; // anonymous user
  333. }
  334. }
  335. else if ($node->uid) {
  336. $user = user_load(array('uid' => $node->uid));
  337. $node->name = $user->name;
  338. $node->picture = $user->picture;
  339. }
  340. $node->changed = time();
  341. // Extract a teaser, if it hasn't been set (e.g. by a module-provided
  342. // 'teaser' form item).
  343. if (!isset($node->teaser)) {
  344. $node->teaser = empty($node->body) ? '' : node_teaser($node->body, $node->format);
  345. // Chop off the teaser from the body if needed.
  346. if (!$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
  347. $node->body = substr($node->body, strlen($node->teaser));
  348. }
  349. }
  350. // Display a preview of the node.
  351. // Previewing alters $node so it needs to be cloned.
  352. if (!form_get_errors()) {
  353. $cloned_node = drupal_clone($node);
  354. $cloned_node->build_mode = NODE_BUILD_PREVIEW;
  355. $output = theme('node_preview', $cloned_node);
  356. }
  357. drupal_set_title(t('Preview'));
  358. return $output;
  359. }
  360. }
  361. /**
  362. * Display a node preview for display during node creation and editing.
  363. *
  364. * @param $node
  365. * The node object which is being previewed.
  366. *
  367. * @ingroup themeable
  368. */
  369. function theme_node_preview($node) {
  370. $output = '<div class="preview">';
  371. $preview_trimmed_version = FALSE;
  372. // Do we need to preview trimmed version of post as well as full version?
  373. if (isset($node->teaser) && isset($node->body)) {
  374. $teaser = trim($node->teaser);
  375. $body = trim(str_replace('<!--break-->', '', $node->body));
  376. // Preview trimmed version if teaser and body will appear different;
  377. // also (edge case) if both teaser and body have been specified by the user
  378. // and are actually the same.
  379. if ($teaser != $body || ($body && strpos($node->body, '<!--break-->') === 0)) {
  380. $preview_trimmed_version = TRUE;
  381. }
  382. }
  383. if ($preview_trimmed_version) {
  384. drupal_set_message(t('The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication.<span class="no-js"> You can insert the delimiter "&lt;!--break--&gt;" (without the quotes) to fine-tune where your post gets split.</span>'));
  385. $output .= '<h3>'. t('Preview trimmed version') .'</h3>';
  386. $output .= node_view(drupal_clone($node), 1, FALSE, 0);
  387. $output .= '<h3>'. t('Preview full version') .'</h3>';
  388. $output .= node_view($node, 0, FALSE, 0);
  389. }
  390. else {
  391. $output .= node_view($node, 0, FALSE, 0);
  392. }
  393. $output .= "</div>\n";
  394. return $output;
  395. }
  396. function node_form_submit($form, &$form_state) {
  397. global $user;
  398. $node = node_form_submit_build_node($form, $form_state);
  399. $insert = empty($node->nid);
  400. node_save($node);
  401. $node_link = l(t('view'), 'node/'. $node->nid);
  402. $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  403. $t_args = array('@type' => node_get_types('name', $node), '%title' => $node->title);
  404. if ($insert) {
  405. watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
  406. drupal_set_message(t('@type %title has been created.', $t_args));
  407. }
  408. else {
  409. watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
  410. drupal_set_message(t('@type %title has been updated.', $t_args));
  411. }
  412. if ($node->nid) {
  413. unset($form_state['rebuild']);
  414. $form_state['nid'] = $node->nid;
  415. $form_state['redirect'] = 'node/'. $node->nid;
  416. }
  417. else {
  418. // In the unlikely case something went wrong on save, the node will be
  419. // rebuilt and node form redisplayed the same way as in preview.
  420. drupal_set_message(t('The post could not be saved.'), 'error');
  421. }
  422. }
  423. /**
  424. * Build a node by processing submitted form values and prepare for a form rebuild.
  425. */
  426. function node_form_submit_build_node($form, &$form_state) {
  427. // Unset any button-level handlers, execute all the form-level submit
  428. // functions to process the form values into an updated node.
  429. unset($form_state['submit_handlers']);
  430. form_execute_handlers('submit', $form, $form_state);
  431. $node = node_submit($form_state['values']);
  432. $form_state['node'] = (array)$node;
  433. $form_state['rebuild'] = TRUE;
  434. return $node;
  435. }
  436. /**
  437. * Menu callback -- ask for confirmation of node deletion
  438. */
  439. function node_delete_confirm(&$form_state, $node) {
  440. $form['nid'] = array(
  441. '#type' => 'value',
  442. '#value' => $node->nid,
  443. );
  444. return confirm_form($form,
  445. t('Are you sure you want to delete %title?', array('%title' => $node->title)),
  446. isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid,
  447. t('This action cannot be undone.'),
  448. t('Delete'),
  449. t('Cancel')
  450. );
  451. }
  452. /**
  453. * Execute node deletion
  454. */
  455. function node_delete_confirm_submit($form, &$form_state) {
  456. if ($form_state['values']['confirm']) {
  457. node_delete($form_state['values']['nid']);
  458. }
  459. $form_state['redirect'] = '<front>';
  460. }
  461. /**
  462. * Generate an overview table of older revisions of a node.
  463. */
  464. function node_revision_overview($node) {
  465. drupal_set_title(t('Revisions for %title', array('%title' => $node->title)));
  466. $header = array(t('Revision'), array('data' => t('Operations'), 'colspan' => 2));
  467. $revisions = node_revision_list($node);
  468. $rows = array();
  469. $revert_permission = FALSE;
  470. if ((user_access('revert revisions') || user_access('administer nodes')) && node_access('update', $node)) {
  471. $revert_permission = TRUE;
  472. }
  473. $delete_permission = FALSE;
  474. if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
  475. $delete_permission = TRUE;
  476. }
  477. foreach ($revisions as $revision) {
  478. $row = array();
  479. $operations = array();
  480. if ($revision->current_vid > 0) {
  481. $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid"), '!username' => theme('username', $revision)))
  482. . (($revision->log != '') ? '<p class="revision-log">'. filter_xss($revision->log) .'</p>' : ''),
  483. 'class' => 'revision-current');
  484. $operations[] = array('data' => theme('placeholder', t('current revision')), 'class' => 'revision-current', 'colspan' => 2);
  485. }
  486. else {
  487. $row[] = t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid/revisions/$revision->vid/view"), '!username' => theme('username', $revision)))
  488. . (($revision->log != '') ? '<p class="revision-log">'. filter_xss($revision->log) .'</p>' : '');
  489. if ($revert_permission) {
  490. $operations[] = l(t('revert'), "node/$node->nid/revisions/$revision->vid/revert");
  491. }
  492. if ($delete_permission) {
  493. $operations[] = l(t('delete'), "node/$node->nid/revisions/$revision->vid/delete");
  494. }
  495. }
  496. $rows[] = array_merge($row, $operations);
  497. }
  498. return theme('table', $header, $rows);
  499. }
  500. /**
  501. * Ask for confirmation of the reversion to prevent against CSRF attacks.
  502. */
  503. function node_revision_revert_confirm($form_state, $node_revision) {
  504. $form['#node_revision'] = $node_revision;
  505. return confirm_form($form, t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => format_date($node_revision->revision_timestamp))), 'node/'. $node_revision->nid .'/revisions', '', t('Revert'), t('Cancel'));
  506. }
  507. function node_revision_revert_confirm_submit($form, &$form_state) {
  508. $node_revision = $form['#node_revision'];
  509. $node_revision->revision = 1;
  510. $node_revision->log = t('Copy of the revision from %date.', array('%date' => format_date($node_revision->revision_timestamp)));
  511. if (module_exists('taxonomy')) {
  512. $node_revision->taxonomy = array_keys($node_revision->taxonomy);
  513. }
  514. node_save($node_revision);
  515. watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
  516. drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_get_types('name', $node_revision), '%title' => $node_revision->title, '%revision-date' => format_date($node_revision->revision_timestamp))));
  517. $form_state['redirect'] = 'node/'. $node_revision->nid .'/revisions';
  518. }
  519. function node_revision_delete_confirm($form_state, $node_revision) {
  520. $form['#node_revision'] = $node_revision;
  521. return confirm_form($form, t('Are you sure you want to delete the revision from %revision-date?', array('%revision-date' => format_date($node_revision->revision_timestamp))), 'node/'. $node_revision->nid .'/revisions', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
  522. }
  523. function node_revision_delete_confirm_submit($form, &$form_state) {
  524. $node_revision = $form['#node_revision'];
  525. db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d", $node_revision->nid, $node_revision->vid);
  526. node_invoke_nodeapi($node_revision, 'delete revision');
  527. watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
  528. drupal_set_message(t('Revision from %revision-date of @type %title has been deleted.', array('%revision-date' => format_date($node_revision->revision_timestamp), '@type' => node_get_types('name', $node_revision), '%title' => $node_revision->title)));
  529. $form_state['redirect'] = 'node/'. $node_revision->nid;
  530. if (db_result(db_query('SELECT COUNT(vid) FROM {node_revisions} WHERE nid = %d', $node_revision->nid)) > 1) {
  531. $form_state['redirect'] .= '/revisions';
  532. }
  533. }
Login or register to post comments