node.pages.inc 24.6 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5 6 7 8 9
 * Callbacks for adding, editing, and deleting content and managing revisions.
 *
 * Also includes validation, submission and other helper functions.
 *
 * @see node_menu()
10 11 12 13
 */


/**
14 15 16
 * Page callback: Presents the node editing form.
 *
 * @see node_menu()
17
 */
18
function node_page_edit($node) {
19
  $type_name = node_type_get_name($node);
20
  drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $type_name, '@title' => $node->title)), PASS_THROUGH);
21
  return drupal_get_form($node->type . '_node_form', $node);
22 23
}

24 25 26 27 28
/**
 * Page callback: Presents the node add form.
 *
 * @see node_menu()
 */
29 30
function node_add_page() {
  $item = menu_get_item();
31
  $content = system_admin_menu_block($item);
32 33 34 35 36
  // Bypass the node/add listing if only one content type is available.
  if (count($content) == 1) {
    $item = array_shift($content);
    drupal_goto($item['href']);
  }
37
  return theme('node_add_list', array('content' => $content));
38 39
}

40
/**
41 42 43 44 45
 * Returns HTML for a list of available node types for node creation.
 *
 * @param $variables
 *   An associative array containing:
 *   - content: An array of content types.
46
 *
47
 * @see node_add_page()
48 49
 * @ingroup themeable
 */
50 51
function theme_node_add_list($variables) {
  $content = $variables['content'];
52 53 54 55 56
  $output = '';

  if ($content) {
    $output = '<dl class="node-type-list">';
    foreach ($content as $item) {
57
      $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
58
      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
59 60 61
    }
    $output .= '</dl>';
  }
62
  else {
63
    $output = '<p>' . t('You have not created any content types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.', array('@create-content' => url('admin/structure/types/add'))) . '</p>';
64
  }
65 66 67 68 69
  return $output;
}


/**
70 71 72 73 74 75 76 77 78
 * Page callback: Provides the node submission form.
 *
 * @param $type
 *   The node type for the submitted node.
 *
 * @return
 *   Returns a node submission form.
 *
 * @see node_menu()
79 80 81 82
 */
function node_add($type) {
  global $user;

83
  $types = node_type_get_types();
84
  $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'langcode' => LANGUAGE_NONE);
85 86
  drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
  $output = drupal_get_form($type . '_node_form', $node);
87 88 89 90

  return $output;
}

91 92 93 94 95 96 97 98
/**
 * Form validation handler for node_form().
 *
 * @see node_form_delete_submit()
 * @see node_form_build_preview()
 * @see node_form_submit()
 * @see node_form_submit_build_node()
 */
99
function node_form_validate($form, &$form_state) {
100 101 102
  // $form_state['node'] contains the actual entity being edited, but we must
  // not update it with form values that have not yet been validated, so we
  // create a pseudo-entity to use during validation.
103
  $node = (object) $form_state['values'];
104
  node_validate($node, $form, $form_state);
105
  entity_form_field_validate('node', $form, $form_state);
106 107 108
}

/**
109 110 111 112 113 114 115 116
 * Form constructor for the node add/edit form.
 *
 * @see node_form_delete_submit()
 * @see node_form_build_preview()
 * @see node_form_validate()
 * @see node_form_submit()
 * @see node_form_submit_build_node()
 * @ingroup forms
117
 */
118
function node_form($form, &$form_state, $node) {
119 120
  global $user;

121 122 123 124 125 126
  // During initial form build, add the node entity to the form state for use
  // during form building and processing. During a rebuild, use what is in the
  // form state.
  if (!isset($form_state['node'])) {
    if (!isset($node->title)) {
      $node->title = NULL;
127
    }
128
    node_object_prepare($node);
129
    $form_state['node'] = $node;
130
  }
131
  else {
132 133 134 135 136 137
    $node = $form_state['node'];
  }

  // Some special stuff when previewing a node.
  if (isset($form_state['node_preview'])) {
    $form['#prefix'] = $form_state['node_preview'];
138
    $node->in_preview = TRUE;
139
  }
140 141 142
  else {
    unset($node->in_preview);
  }
143

144 145 146
  // Override the default CSS class name, since the user-defined node type name
  // in 'TYPE-node-form' potentially clashes with third-party class names.
  $form['#attributes']['class'][0] = drupal_html_class('node-' . $node->type . '-form');
147

148 149
  // Basic node information.
  // These elements are just values so they are not even sent to the client.
150
  foreach (array('nid', 'vid', 'uid', 'created', 'type') as $key) {
151
    $form[$key] = array(
152
      '#type' => 'value',
153 154
      '#value' => isset($node->$key) ? $node->$key : NULL,
    );
155 156 157
  }

  // Changed must be sent to the client, for later overwrite error checking.
158
  $form['changed'] = array(
159
    '#type' => 'hidden',
160 161
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
  );
162 163
  // Invoke hook_form() to get the node-specific bits. Can't use node_invoke(),
  // because hook_form() needs to be able to receive $form_state by reference.
164 165
  // @todo hook_form() implementations are unable to add #validate or #submit
  //   handlers to the form buttons below. Remove hook_form() entirely.
166 167
  $function = node_type_get_base($node) . '_form';
  if (function_exists($function) && ($extra = $function($node, $form_state))) {
168 169
    $form = array_merge_recursive($form, $extra);
  }
170 171 172
  // If the node type has a title, and the node type form defined no special
  // weight for it, we default to a weight of -5 for consistency.
  if (isset($form['title']) && !isset($form['title']['#weight'])) {
173 174
    $form['title']['#weight'] = -5;
  }
175
  // @todo D8: Remove. Modules should access the node using $form_state['node'].
176 177
  $form['#node'] = $node;

178 179 180 181 182 183
  if (variable_get('node_type_language_' . $node->type, 0) && module_exists('language')) {
    $languages = language_list(TRUE);
    $language_options = array();
    foreach ($languages as $langcode => $language) {
      $language_options[$langcode] = $language->name;
    }
184
    $form['langcode'] = array(
185 186
      '#type' => 'select',
      '#title' => t('Language'),
187
      '#default_value' => (isset($node->langcode) ? $node->langcode : ''),
188 189 190 191 192
      '#options' => $language_options,
      '#empty_value' => LANGUAGE_NONE,
    );
  }
  else {
193
    $form['langcode'] = array(
194 195 196
      '#type' => 'value',
      // New nodes without multilingual support get the default language, old
      // nodes keep their language if language.module is not available.
197
      '#value' => !isset($form['#node']->nid) ? language_default()->langcode : $node->langcode,
198 199 200
    );
  }

201 202
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
203
    '#weight' => 99,
204 205
  );

206 207
  // Add a log field if the "Create new revision" option is checked, or if the
  // current user has the ability to check that option.
208 209 210 211 212 213 214
  $form['revision_information'] = array(
    '#type' => 'fieldset',
    '#title' => t('Revision information'),
    '#collapsible' => TRUE,
    // Collapsed by default when "Create new revision" is unchecked
    '#collapsed' => !$node->revision,
    '#group' => 'additional_settings',
215 216 217
    '#attributes' => array(
      'class' => array('node-form-revision-information'),
    ),
218 219 220 221 222 223 224 225 226 227
    '#attached' => array(
      'js' => array(drupal_get_path('module', 'node') . '/node.js'),
    ),
    '#weight' => 20,
    '#access' => $node->revision || user_access('administer nodes'),
  );
  $form['revision_information']['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $node->revision,
228 229 230 231 232 233 234
    '#access' => user_access('administer nodes'),
  );
  // Check the revision log checkbox when the log textarea is filled in.
  // This must not happen if "Create new revision" is enabled by default, since
  // the state would auto-disable the checkbox otherwise.
  if (!$node->revision) {
    $form['revision_information']['revision']['#states'] = array(
235 236
      'checked' => array(
        'textarea[name="log"]' => array('empty' => FALSE),
237
      ),
238 239
    );
  }
240 241 242 243 244
  $form['revision_information']['log'] = array(
    '#type' => 'textarea',
    '#title' => t('Revision log message'),
    '#rows' => 4,
    '#default_value' => !empty($node->log) ? $node->log : '',
245
    '#description' => t('Briefly describe the changes you have made.'),
246
  );
247 248 249 250 251 252 253 254

  // Node author information for administrators
  $form['author'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Authoring information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
255
    '#group' => 'additional_settings',
256 257 258
    '#attributes' => array(
      'class' => array('node-form-author'),
    ),
259
    '#attached' => array(
260 261 262 263 264 265 266
      'js' => array(
        drupal_get_path('module', 'node') . '/node.js',
        array(
          'type' => 'setting',
          'data' => array('anonymous' => variable_get('anonymous', t('Anonymous'))),
        ),
      ),
267
    ),
268
    '#weight' => 90,
269
  );
270
  $form['author']['name'] = array(
271 272 273 274
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
275
    '#default_value' => !empty($node->name) ? $node->name : '',
276
    '#weight' => -1,
277 278 279
    '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  );
  $form['author']['date'] = array(
280 281 282
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#maxlength' => 25,
283
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array('%time' => !empty($node->date) ? date_format(date_create($node->date), 'Y-m-d H:i:s O') : format_date($node->created, 'custom', 'Y-m-d H:i:s O'), '%timezone' => !empty($node->date) ? date_format(date_create($node->date), 'O') : format_date($node->created, 'custom', 'O'))),
284
    '#default_value' => !empty($node->date) ? $node->date : '',
285
  );
286 287 288 289 290 291 292 293

  // Node options for administrators
  $form['options'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
294
    '#group' => 'additional_settings',
295 296 297
    '#attributes' => array(
      'class' => array('node-form-options'),
    ),
298 299 300
    '#attached' => array(
      'js' => array(drupal_get_path('module', 'node') . '/node.js'),
    ),
301
    '#weight' => 95,
302
  );
303
  $form['options']['status'] = array(
304 305
    '#type' => 'checkbox',
    '#title' => t('Published'),
306 307 308
    '#default_value' => $node->status,
  );
  $form['options']['promote'] = array(
309 310
    '#type' => 'checkbox',
    '#title' => t('Promoted to front page'),
311 312 313
    '#default_value' => $node->promote,
  );
  $form['options']['sticky'] = array(
314 315
    '#type' => 'checkbox',
    '#title' => t('Sticky at top of lists'),
316 317
    '#default_value' => $node->sticky,
  );
318 319

  // Add the buttons.
320
  $form['actions'] = array('#type' => 'actions');
321
  $form['actions']['submit'] = array(
322
    '#type' => 'submit',
323
    '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || (!form_get_errors() && isset($form_state['node_preview'])),
324
    '#value' => t('Save'),
325
    '#weight' => 5,
326
    '#submit' => array('node_form_submit'),
327
  );
328
  $form['actions']['preview'] = array(
329
    '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_DISABLED,
330
    '#type' => 'submit',
331
    '#value' => t('Preview'),
332
    '#weight' => 10,
333
    '#submit' => array('node_form_build_preview'),
334 335
  );
  if (!empty($node->nid) && node_access('delete', $node)) {
336
    $form['actions']['delete'] = array(
337 338 339 340 341 342
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 15,
      '#submit' => array('node_form_delete_submit'),
    );
  }
343 344 345 346 347 348
  // This form uses a button-level #submit handler for the form's main submit
  // action. node_form_submit() manually invokes all form-level #submit handlers
  // of the form. Without explicitly setting #submit, Form API would auto-detect
  // node_form_submit() as submit handler, but that is the button-level #submit
  // handler for the 'Save' action. To maintain backwards compatibility, a
  // #submit handler is auto-suggested for custom node type modules.
349
  $form['#validate'][] = 'node_form_validate';
350 351 352 353
  if (!isset($form['#submit']) && function_exists($node->type . '_node_form_submit')) {
    $form['#submit'][] = $node->type . '_node_form_submit';
  }
  $form += array('#submit' => array());
354

355
  field_attach_form('node', $node, $form, $form_state, $node->langcode);
356 357 358 359
  return $form;
}

/**
360 361 362 363 364 365
 * Form submission handler for the 'Delete' button for node_form().
 *
 * @see node_form_build_preview()
 * @see node_form_validate()
 * @see node_form_submit()
 * @see node_form_submit_build_node()
366 367
 */
function node_form_delete_submit($form, &$form_state) {
368
  $destination = array();
369
  if (isset($_GET['destination'])) {
370
    $destination = drupal_get_destination();
371
    unset($_GET['destination']);
372 373
  }
  $node = $form['#node'];
374
  $form_state['redirect'] = array('node/' . $node->nid . '/delete', array('query' => $destination));
375 376
}

377 378 379 380 381 382 383 384
/**
 * Form submission handler for the 'Preview' button for node_form().
 *
 * @see node_form_delete_submit()
 * @see node_form_validate()
 * @see node_form_submit()
 * @see node_form_submit_build_node()
 */
385
function node_form_build_preview($form, &$form_state) {
386
  $node = node_form_submit_build_node($form, $form_state);
387
  $form_state['node_preview'] = node_preview($node);
388
  $form_state['rebuild'] = TRUE;
389 390 391
}

/**
392 393 394 395 396 397 398 399 400
 * Generates a node preview.
 *
 * @param $node
 *   The node to preview.
 *
 * @return
 *   Themed node preview.
 *
 * @see node_form_build_preview()
401
 */
402
function node_preview($node) {
403
  if (node_access('create', $node) || node_access('update', $node)) {
404
    _field_invoke_multiple('load', 'node', array($node->nid => $node));
405
    // Load the user's name when needed.
406 407 408
    if (isset($node->name)) {
      // The use of isset() is mandatory in the context of user IDs, because
      // user ID 0 denotes the anonymous user.
409
      if ($user = user_load_by_name($node->name)) {
410 411 412 413 414 415 416
        $node->uid = $user->uid;
        $node->picture = $user->picture;
      }
      else {
        $node->uid = 0; // anonymous user
      }
    }
417
    elseif ($node->uid) {
418
      $user = user_load($node->uid);
419 420 421 422
      $node->name = $user->name;
      $node->picture = $user->picture;
    }

423
    $node->changed = REQUEST_TIME;
424 425
    $nodes = array($node->nid => $node);
    field_attach_prepare_view('node', $nodes, 'full');
426

427
    // Display a preview of the node.
428
    if (!form_get_errors()) {
429 430 431
      $node->in_preview = TRUE;
      $output = theme('node_preview', array('node' => $node));
      unset($node->in_preview);
432
    }
433
    drupal_set_title(t('Preview'), PASS_THROUGH);
434 435 436 437 438 439

    return $output;
  }
}

/**
440
 * Returns HTML for a node preview for display during node creation and editing.
441
 *
442 443 444
 * @param $variables
 *   An associative array containing:
 *   - node: The node object which is being previewed.
445
 *
446
 * @see node_preview()
447
 * @ingroup themeable
448
 */
449 450 451
function theme_node_preview($variables) {
  $node = $variables['node'];

452
  $output = '<div class="preview">';
453

454
  $preview_trimmed_version = FALSE;
455

456 457 458 459
  $elements = node_view(clone $node, 'teaser');
  $trimmed = drupal_render($elements);
  $elements = node_view($node, 'full');
  $full = drupal_render($elements);
460 461 462

  // Do we need to preview trimmed version of post as well as full version?
  if ($trimmed != $full) {
463
    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>'));
464
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
465
    $output .= $trimmed;
466
    $output .= '<h3>' . t('Preview full version') . '</h3>';
467
    $output .= $full;
468 469
  }
  else {
470
    $output .= $full;
471 472 473 474 475 476
  }
  $output .= "</div>\n";

  return $output;
}

477 478 479 480 481 482 483 484
/**
 * Form submission handler that saves the node for node_form().
 *
 * @see node_form_delete_submit()
 * @see node_form_build_preview()
 * @see node_form_validate()
 * @see node_form_submit_build_node()
 */
485
function node_form_submit($form, &$form_state) {
486
  $node = node_form_submit_build_node($form, $form_state);
487 488
  $insert = empty($node->nid);
  node_save($node);
489
  $node_link = l(t('view'), 'node/' . $node->nid);
490 491
  $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  $t_args = array('@type' => node_type_get_name($node), '%title' => $node->title);
492 493

  if ($insert) {
494
    watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
495
    drupal_set_message(t('@type %title has been created.', $t_args));
496 497
  }
  else {
498
    watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
499
    drupal_set_message(t('@type %title has been updated.', $t_args));
500 501
  }
  if ($node->nid) {
502
    $form_state['values']['nid'] = $node->nid;
503
    $form_state['nid'] = $node->nid;
504
    $form_state['redirect'] = 'node/' . $node->nid;
505 506 507 508
  }
  else {
    // In the unlikely case something went wrong on save, the node will be
    // rebuilt and node form redisplayed the same way as in preview.
509
    drupal_set_message(t('The post could not be saved.'), 'error');
510
    $form_state['rebuild'] = TRUE;
511
  }
512 513
  // Clear the page and block caches.
  cache_clear_all();
514 515 516
}

/**
517 518
 * Updates the form state's node entity by processing this submission's values.
 *
519
 * This is the default builder function for the node form. It is called
520 521 522 523 524 525
 * during the "Save" and "Preview" submit handlers to retrieve the entity to
 * save or preview. This function can also be called by a "Next" button of a
 * wizard to update the form state's entity with the current step's values
 * before proceeding to the next step.
 *
 * @see node_form()
526 527 528 529
 * @see node_form_delete_submit()
 * @see node_form_build_preview()
 * @see node_form_validate()
 * @see node_form_submit()
530 531
 */
function node_form_submit_build_node($form, &$form_state) {
532 533 534 535 536 537 538
  // @todo Legacy support for modules that extend the node form with form-level
  //   submit handlers that adjust $form_state['values'] prior to those values
  //   being used to update the entity. Module authors are encouraged to instead
  //   adjust the node directly within a hook_node_submit() implementation. For
  //   Drupal 8, evaluate whether the pattern of triggering form-level submit
  //   handlers during button-level submit processing is worth supporting
  //   properly, and if so, add a Form API function for doing so.
539
  unset($form_state['submit_handlers']);
540 541
  form_execute_handlers('submit', $form, $form_state);

542 543
  $node = $form_state['node'];
  entity_form_submit_build_entity('node', $node, $form, $form_state);
Dries's avatar
 
Dries committed
544

545 546 547 548 549
  node_submit($node);
  foreach (module_implements('node_submit') as $module) {
    $function = $module . '_node_submit';
    $function($node, $form, $form_state);
  }
550 551 552 553
  return $node;
}

/**
554 555 556
 * Page callback: Form constructor for node deletion confirmation form.
 *
 * @see node_menu()
557
 */
558
function node_delete_confirm($form, &$form_state, $node) {
559 560 561
  $form['#node'] = $node;
  // Always provide entity id in the same form key as in the entity edit form.
  $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
562
  return confirm_form($form,
563
    t('Are you sure you want to delete %title?', array('%title' => $node->title)),
564
    'node/' . $node->nid,
565
    t('This action cannot be undone.'),
566
    t('Delete'),
567 568
    t('Cancel')
  );
569 570 571
}

/**
572
 * Form submission handler for node_delete_confirm().
573 574 575
 */
function node_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
576
    $node = node_load($form_state['values']['nid']);
577
    node_delete($form_state['values']['nid']);
578 579
    watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->title));
    drupal_set_message(t('@type %title has been deleted.', array('@type' => node_type_get_name($node), '%title' => $node->title)));
580 581 582 583 584 585
  }

  $form_state['redirect'] = '<front>';
}

/**
586 587 588
 * Page callback: Generates an overview table of older revisions of a node.
 *
 * @see node_menu()
589
 */
590
function node_revision_overview($node) {
591
  drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
592 593 594 595 596 597 598 599 600 601 602

  $header = array(t('Revision'), array('data' => t('Operations'), 'colspan' => 2));

  $revisions = node_revision_list($node);

  $rows = array();
  $revert_permission = FALSE;
  if ((user_access('revert revisions') || user_access('administer nodes')) && node_access('update', $node)) {
    $revert_permission = TRUE;
  }
  $delete_permission = FALSE;
603
  if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
604 605 606 607 608 609 610
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
    $operations = array();

    if ($revision->current_vid > 0) {
611
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'short'), "node/$node->nid"), '!username' => theme('username', array('account' => $revision))))
612
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
613
                     'class' => array('revision-current'));
614
      $operations[] = array('data' => drupal_placeholder(t('current revision')), 'class' => array('revision-current'), 'colspan' => 2);
615 616
    }
    else {
617
      $row[] = t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'short'), "node/$node->nid/revisions/$revision->vid/view"), '!username' => theme('username', array('account' => $revision))))
618
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
619 620 621 622 623 624 625 626 627
      if ($revert_permission) {
        $operations[] = l(t('revert'), "node/$node->nid/revisions/$revision->vid/revert");
      }
      if ($delete_permission) {
        $operations[] = l(t('delete'), "node/$node->nid/revisions/$revision->vid/delete");
      }
    }
    $rows[] = array_merge($row, $operations);
  }
628

629 630 631 632 633
  $build['node_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  );
634

635
  return $build;
636 637
}

638
/**
639 640 641 642 643 644
 * Page callback: Form constructor for the reversion confirmation form.
 *
 * This form prevents against CSRF attacks.
 *
 * @see node_menu()
 * @see node_revision_revert_confirm_submit()
645
 */
646
function node_revision_revert_confirm($form, $form_state, $node_revision) {
647
  $form['#node_revision'] = $node_revision;
648
  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'));
649 650
}

651 652 653
/**
 * Form submission handler for node_revision_revert_confirm().
 */
654 655 656 657 658 659 660
function node_revision_revert_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
  $node_revision->revision = 1;
  $node_revision->log = t('Copy of the revision from %date.', array('%date' => format_date($node_revision->revision_timestamp)));

  node_save($node_revision);

661 662
  watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
  drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_type_get_name($node_revision), '%title' => $node_revision->title, '%revision-date' => format_date($node_revision->revision_timestamp))));
663
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
664
}
665

666 667 668 669 670 671 672 673
/**
 * Page callback: Form constructor for the revision deletion confirmation form.
 *
 * This form prevents against CSRF attacks.
 *
 * @see node_menu()
 * @see node_revision_delete_confirm_submit()
 */
674
function node_revision_delete_confirm($form, $form_state, $node_revision) {
675
  $form['#node_revision'] = $node_revision;
676
  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'));
677 678
}

679 680 681
/**
 * Form submission handler for node_revision_delete_confirm().
 */
682 683
function node_revision_delete_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
684 685
  node_revision_delete($node_revision->vid);

686 687
  watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
  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_type_get_name($node_revision), '%title' => $node_revision->title)));
688
  $form_state['redirect'] = 'node/' . $node_revision->nid;
689
  if (db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid', array(':nid' => $node_revision->nid))->fetchField() > 1) {
690 691 692
    $form_state['redirect'] .= '/revisions';
  }
}