node.pages.inc

You are here

  1. 7 modules/node/node.pages.inc
  2. 6 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

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