node.pages.inc 24.9 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 17 18
 * Page callback: Presents the node editing form.
 *
 * Path: node/%node/edit
 *
 * @see node_menu()
19
 */
20
function node_page_edit($node) {
21
  $type_name = node_type_get_name($node);
22
  drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => $type_name, '@title' => $node->title)), PASS_THROUGH);
23
  return drupal_get_form($node->type . '_node_form', $node);
24 25
}

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

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

  if ($content) {
    $output = '<dl class="node-type-list">';
    foreach ($content as $item) {
61
      $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
62
      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
63 64 65
    }
    $output .= '</dl>';
  }
66
  else {
67
    $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>';
68
  }
69 70 71 72 73
  return $output;
}


/**
74 75 76 77 78 79 80 81 82 83 84
 * Page callback: Provides the node submission form.
 *
 * Path: 'node/add/' . $type_url_str (part of a foreach)
 *
 * @param $type
 *   The node type for the submitted node.
 *
 * @return
 *   Returns a node submission form.
 *
 * @see node_menu()
85 86 87 88
 */
function node_add($type) {
  global $user;

89
  $types = node_type_get_types();
90 91 92
  $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => LANGUAGE_NONE);
  drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
  $output = drupal_get_form($type . '_node_form', $node);
93 94 95 96

  return $output;
}

97 98 99 100 101 102 103 104
/**
 * 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()
 */
105
function node_form_validate($form, &$form_state) {
106 107 108
  // $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.
109
  $node = (object) $form_state['values'];
110
  node_validate($node, $form, $form_state);
111
  entity_form_field_validate('node', $form, $form_state);
112 113 114
}

/**
115 116 117 118 119 120 121 122
 * 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
123
 */
124
function node_form($form, &$form_state, $node) {
125 126
  global $user;

127 128 129 130 131 132
  // 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;
133
    }
134
    node_object_prepare($node);
135
    $form_state['node'] = $node;
136
  }
137
  else {
138 139 140 141 142 143
    $node = $form_state['node'];
  }

  // Some special stuff when previewing a node.
  if (isset($form_state['node_preview'])) {
    $form['#prefix'] = $form_state['node_preview'];
144
    $node->in_preview = TRUE;
145
  }
146 147 148
  else {
    unset($node->in_preview);
  }
149

150 151 152
  // 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');
153

154 155
  // Basic node information.
  // These elements are just values so they are not even sent to the client.
156
  foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as $key) {
157
    $form[$key] = array(
158
      '#type' => 'value',
159 160
      '#value' => isset($node->$key) ? $node->$key : NULL,
    );
161 162 163
  }

  // Changed must be sent to the client, for later overwrite error checking.
164
  $form['changed'] = array(
165
    '#type' => 'hidden',
166 167
    '#default_value' => isset($node->changed) ? $node->changed : NULL,
  );
168 169
  // 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.
170 171
  // @todo hook_form() implementations are unable to add #validate or #submit
  //   handlers to the form buttons below. Remove hook_form() entirely.
172 173
  $function = node_type_get_base($node) . '_form';
  if (function_exists($function) && ($extra = $function($node, $form_state))) {
174 175
    $form = array_merge_recursive($form, $extra);
  }
176 177 178
  // 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'])) {
179 180
    $form['title']['#weight'] = -5;
  }
181
  // @todo D8: Remove. Modules should access the node using $form_state['node'].
182 183
  $form['#node'] = $node;

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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;
    }
    $form['language'] = array(
      '#type' => 'select',
      '#title' => t('Language'),
      '#default_value' => (isset($node->language) ? $node->language : ''),
      '#options' => $language_options,
      '#empty_value' => LANGUAGE_NONE,
    );
  }
  else {
    $form['language'] = array(
      '#type' => 'value',
      // New nodes without multilingual support get the default language, old
      // nodes keep their language if language.module is not available.
      '#value' => !isset($form['#node']->nid) ? language_default()->langcode : $node->language,
    );
  }

207 208
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
209
    '#weight' => 99,
210 211
  );

212 213
  // Add a log field if the "Create new revision" option is checked, or if the
  // current user has the ability to check that option.
214 215 216 217 218 219 220
  $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',
221 222 223
    '#attributes' => array(
      'class' => array('node-form-revision-information'),
    ),
224 225 226 227 228 229 230 231 232 233
    '#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,
234 235 236 237 238 239 240
    '#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(
241 242
      'checked' => array(
        'textarea[name="log"]' => array('empty' => FALSE),
243
      ),
244 245
    );
  }
246 247 248 249 250
  $form['revision_information']['log'] = array(
    '#type' => 'textarea',
    '#title' => t('Revision log message'),
    '#rows' => 4,
    '#default_value' => !empty($node->log) ? $node->log : '',
251
    '#description' => t('Briefly describe the changes you have made.'),
252
  );
253 254 255 256 257 258 259 260

  // Node author information for administrators
  $form['author'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer nodes'),
    '#title' => t('Authoring information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
261
    '#group' => 'additional_settings',
262 263 264
    '#attributes' => array(
      'class' => array('node-form-author'),
    ),
265
    '#attached' => array(
266 267 268 269 270 271 272
      'js' => array(
        drupal_get_path('module', 'node') . '/node.js',
        array(
          'type' => 'setting',
          'data' => array('anonymous' => variable_get('anonymous', t('Anonymous'))),
        ),
      ),
273
    ),
274
    '#weight' => 90,
275
  );
276
  $form['author']['name'] = array(
277 278 279 280
    '#type' => 'textfield',
    '#title' => t('Authored by'),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
281
    '#default_value' => !empty($node->name) ? $node->name : '',
282
    '#weight' => -1,
283 284 285
    '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
  );
  $form['author']['date'] = array(
286 287 288
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#maxlength' => 25,
289
    '#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'))),
290
    '#default_value' => !empty($node->date) ? $node->date : '',
291
  );
292 293 294 295 296 297 298 299

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

  // Add the buttons.
326
  $form['actions'] = array('#type' => 'actions');
327
  $form['actions']['submit'] = array(
328
    '#type' => 'submit',
329
    '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || (!form_get_errors() && isset($form_state['node_preview'])),
330
    '#value' => t('Save'),
331
    '#weight' => 5,
332
    '#submit' => array('node_form_submit'),
333
  );
334
  $form['actions']['preview'] = array(
335
    '#access' => variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_DISABLED,
336
    '#type' => 'submit',
337
    '#value' => t('Preview'),
338
    '#weight' => 10,
339
    '#submit' => array('node_form_build_preview'),
340 341
  );
  if (!empty($node->nid) && node_access('delete', $node)) {
342
    $form['actions']['delete'] = array(
343 344 345 346 347 348
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#weight' => 15,
      '#submit' => array('node_form_delete_submit'),
    );
  }
349 350 351 352 353 354
  // 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.
355
  $form['#validate'][] = 'node_form_validate';
356 357 358 359
  if (!isset($form['#submit']) && function_exists($node->type . '_node_form_submit')) {
    $form['#submit'][] = $node->type . '_node_form_submit';
  }
  $form += array('#submit' => array());
360

361
  field_attach_form('node', $node, $form, $form_state, $node->language);
362 363 364 365
  return $form;
}

/**
366 367 368 369 370 371
 * 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()
372 373
 */
function node_form_delete_submit($form, &$form_state) {
374
  $destination = array();
375
  if (isset($_GET['destination'])) {
376
    $destination = drupal_get_destination();
377
    unset($_GET['destination']);
378 379
  }
  $node = $form['#node'];
380
  $form_state['redirect'] = array('node/' . $node->nid . '/delete', array('query' => $destination));
381 382
}

383 384 385 386 387 388 389 390
/**
 * 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()
 */
391
function node_form_build_preview($form, &$form_state) {
392
  $node = node_form_submit_build_node($form, $form_state);
393
  $form_state['node_preview'] = node_preview($node);
394
  $form_state['rebuild'] = TRUE;
395 396 397
}

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

429
    $node->changed = REQUEST_TIME;
430 431
    $nodes = array($node->nid => $node);
    field_attach_prepare_view('node', $nodes, 'full');
432

433
    // Display a preview of the node.
434
    if (!form_get_errors()) {
435 436 437
      $node->in_preview = TRUE;
      $output = theme('node_preview', array('node' => $node));
      unset($node->in_preview);
438
    }
439
    drupal_set_title(t('Preview'), PASS_THROUGH);
440 441 442 443 444 445

    return $output;
  }
}

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

458
  $output = '<div class="preview">';
459

460
  $preview_trimmed_version = FALSE;
461

462 463 464 465
  $elements = node_view(clone $node, 'teaser');
  $trimmed = drupal_render($elements);
  $elements = node_view($node, 'full');
  $full = drupal_render($elements);
466 467 468

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

  return $output;
}

483 484 485 486 487 488 489 490
/**
 * 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()
 */
491
function node_form_submit($form, &$form_state) {
492
  $node = node_form_submit_build_node($form, $form_state);
493 494
  $insert = empty($node->nid);
  node_save($node);
495
  $node_link = l(t('view'), 'node/' . $node->nid);
496 497
  $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  $t_args = array('@type' => node_type_get_name($node), '%title' => $node->title);
498 499

  if ($insert) {
500
    watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
501
    drupal_set_message(t('@type %title has been created.', $t_args));
502 503
  }
  else {
504
    watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
505
    drupal_set_message(t('@type %title has been updated.', $t_args));
506 507
  }
  if ($node->nid) {
508
    $form_state['values']['nid'] = $node->nid;
509
    $form_state['nid'] = $node->nid;
510
    $form_state['redirect'] = 'node/' . $node->nid;
511 512 513 514
  }
  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.
515
    drupal_set_message(t('The post could not be saved.'), 'error');
516
    $form_state['rebuild'] = TRUE;
517
  }
518 519
  // Clear the page and block caches.
  cache_clear_all();
520 521 522
}

/**
523 524
 * Updates the form state's node entity by processing this submission's values.
 *
525
 * This is the default builder function for the node form. It is called
526 527 528 529 530 531
 * 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()
532 533 534 535
 * @see node_form_delete_submit()
 * @see node_form_build_preview()
 * @see node_form_validate()
 * @see node_form_submit()
536 537
 */
function node_form_submit_build_node($form, &$form_state) {
538 539 540 541 542 543 544
  // @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.
545
  unset($form_state['submit_handlers']);
546 547
  form_execute_handlers('submit', $form, $form_state);

548 549
  $node = $form_state['node'];
  entity_form_submit_build_entity('node', $node, $form, $form_state);
Dries's avatar
 
Dries committed
550

551 552 553 554 555
  node_submit($node);
  foreach (module_implements('node_submit') as $module) {
    $function = $module . '_node_submit';
    $function($node, $form, $form_state);
  }
556 557 558 559
  return $node;
}

/**
560 561 562 563 564
 * Page callback: Form constructor for node deletion confirmation form.
 *
 * Path: node/%node/delete
 *
 * @see node_menu()
565
 */
566
function node_delete_confirm($form, &$form_state, $node) {
567 568 569
  $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);
570
  return confirm_form($form,
571
    t('Are you sure you want to delete %title?', array('%title' => $node->title)),
572
    'node/' . $node->nid,
573
    t('This action cannot be undone.'),
574
    t('Delete'),
575 576
    t('Cancel')
  );
577 578 579
}

/**
580
 * Form submission handler for node_delete_confirm().
581 582 583
 */
function node_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
584
    $node = node_load($form_state['values']['nid']);
585
    node_delete($form_state['values']['nid']);
586 587
    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)));
588 589 590 591 592 593
  }

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

/**
594 595 596 597 598
 * Page callback: Generates an overview table of older revisions of a node.
 *
 * Path: node/%node/revisions
 *
 * @see node_menu()
599
 */
600
function node_revision_overview($node) {
601
  drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
602 603 604 605 606 607 608 609 610 611 612

  $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;
613
  if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
614 615 616 617 618 619 620
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
    $operations = array();

    if ($revision->current_vid > 0) {
621
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'short'), "node/$node->nid"), '!username' => theme('username', array('account' => $revision))))
622
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
623
                     'class' => array('revision-current'));
624
      $operations[] = array('data' => drupal_placeholder(t('current revision')), 'class' => array('revision-current'), 'colspan' => 2);
625 626
    }
    else {
627
      $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))))
628
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
629 630 631 632 633 634 635 636 637
      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);
  }
638

639 640 641 642 643
  $build['node_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
  );
644

645
  return $build;
646 647
}

648
/**
649 650 651 652 653 654 655 656
 * Page callback: Form constructor for the reversion confirmation form.
 *
 * Path: node/%node/revisions/%/revert
 *
 * This form prevents against CSRF attacks.
 *
 * @see node_menu()
 * @see node_revision_revert_confirm_submit()
657
 */
658
function node_revision_revert_confirm($form, $form_state, $node_revision) {
659
  $form['#node_revision'] = $node_revision;
660
  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'));
661 662
}

663 664 665
/**
 * Form submission handler for node_revision_revert_confirm().
 */
666 667 668 669 670 671 672
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);

673 674
  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))));
675
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
676
}
677

678 679 680 681 682 683 684 685 686 687
/**
 * Page callback: Form constructor for the revision deletion confirmation form.
 *
 * Path: node/%node/revisions/%/delete
 *
 * This form prevents against CSRF attacks.
 *
 * @see node_menu()
 * @see node_revision_delete_confirm_submit()
 */
688
function node_revision_delete_confirm($form, $form_state, $node_revision) {
689
  $form['#node_revision'] = $node_revision;
690
  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'));
691 692
}

693 694 695
/**
 * Form submission handler for node_revision_delete_confirm().
 */
696 697
function node_revision_delete_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
698 699
  node_revision_delete($node_revision->vid);

700 701
  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)));
702
  $form_state['redirect'] = 'node/' . $node_revision->nid;
703
  if (db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid', array(':nid' => $node_revision->nid))->fetchField() > 1) {
704 705 706
    $form_state['redirect'] .= '/revisions';
  }
}