node.pages.inc 26.2 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
use Drupal\node\Node;
13 14

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

25
/**
26 27 28
 * Page callback: Displays add content links for available content types.
 *
 * Redirects to node/add/[type] if only one content type is available.
29 30 31
 *
 * @see node_menu()
 */
32
function node_add_page() {
33 34 35 36 37 38 39
  $content = array();
  // Only use node types the user has access to.
  foreach (node_type_get_types() as $type) {
    if (node_hook($type->type, 'form') && node_access('create', $type->type)) {
      $content[$type->type] = $type;
    }
  }
40 41
  // Bypass the node/add listing if only one content type is available.
  if (count($content) == 1) {
42 43
    $type = array_shift($content);
    drupal_goto('node/add/' . $type->type);
44
  }
45
  return array('#theme' => 'node_add_list', '#content' => $content);
46 47
}

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

  if ($content) {
    $output = '<dl class="node-type-list">';
64 65 66
    foreach ($content as $type) {
      $output .= '<dt>' . l($type->name, 'node/add/' . $type->type) . '</dt>';
      $output .= '<dd>' . filter_xss_admin($type->description) . '</dd>';
67 68 69
    }
    $output .= '</dl>';
  }
70
  else {
71
    $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>';
72
  }
73 74 75 76 77
  return $output;
}


/**
78 79 80 81 82 83 84 85 86
 * 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()
87 88 89 90
 */
function node_add($type) {
  global $user;

91
  $types = node_type_get_types();
92 93 94 95
  $node = entity_create('node', array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $type,
96
    'langcode' => node_type_get_default_langcode($type)
97
  ));
98 99
  drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH);
  $output = drupal_get_form($type . '_node_form', $node);
100 101 102 103

  return $output;
}

104 105 106 107 108 109 110 111
/**
 * 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()
 */
112
function node_form_validate($form, &$form_state) {
113 114 115
  // $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.
116 117 118 119
  $node = clone $form_state['node'];
  foreach ($form_state['values'] as $key => $value) {
    $node->{$key} = $value;
  }
120
  node_validate($node, $form, $form_state);
121
  entity_form_field_validate('node', $form, $form_state);
122 123 124
}

/**
125 126 127 128 129 130 131 132
 * 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
133
 */
134
function node_form($form, &$form_state, Node $node) {
135 136
  global $user;

137 138 139 140
  // 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'])) {
141
    node_object_prepare($node);
142
    $form_state['node'] = $node;
143
  }
144
  else {
145 146 147 148 149 150
    $node = $form_state['node'];
  }

  // Some special stuff when previewing a node.
  if (isset($form_state['node_preview'])) {
    $form['#prefix'] = $form_state['node_preview'];
151
    $node->in_preview = TRUE;
152
  }
153 154 155
  else {
    unset($node->in_preview);
  }
156

157 158 159
  // 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');
160

161 162
  // Basic node information.
  // These elements are just values so they are not even sent to the client.
163
  foreach (array('nid', 'vid', 'uid', 'created', 'type') as $key) {
164
    $form[$key] = array(
165
      '#type' => 'value',
166 167
      '#value' => isset($node->$key) ? $node->$key : NULL,
    );
168 169 170
  }

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

191
  if (module_exists('language')) {
192
    $languages = language_list(LANGUAGE_ALL);
193 194
    $language_options = array();
    foreach ($languages as $langcode => $language) {
195 196
      // Make locked languages appear special in the list.
      $language_options[$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
197
    }
198
    $form['langcode'] = array(
199 200
      '#type' => 'select',
      '#title' => t('Language'),
201
      '#default_value' => $node->langcode,
202
      '#options' => $language_options,
203
      '#access' => !variable_get('node_type_language_hidden_' . $node->type, TRUE),
204 205 206
    );
  }
  else {
207
    $form['langcode'] = array(
208
      '#type' => 'value',
209
      '#value' => $node->langcode,
210 211 212
    );
  }

213 214
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
215
    '#weight' => 99,
216 217
  );

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

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

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

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

367
  field_attach_form('node', $node, $form, $form_state, $node->langcode);
368 369 370 371
  return $form;
}

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

389 390 391 392 393 394 395 396
/**
 * 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()
 */
397
function node_form_build_preview($form, &$form_state) {
398
  $node = node_form_submit_build_node($form, $form_state);
399
  $form_state['node_preview'] = node_preview($node);
400
  $form_state['rebuild'] = TRUE;
401 402 403
}

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

435
    $node->changed = REQUEST_TIME;
436 437
    $nodes = array($node->nid => $node);
    field_attach_prepare_view('node', $nodes, 'full');
438

439
    // Display a preview of the node.
440
    if (!form_get_errors()) {
441 442 443
      $node->in_preview = TRUE;
      $output = theme('node_preview', array('node' => $node));
      unset($node->in_preview);
444
    }
445
    drupal_set_title(t('Preview'), PASS_THROUGH);
446 447 448 449 450 451

    return $output;
  }
}

/**
452
 * Returns HTML for a node preview for display during node creation and editing.
453
 *
454 455
 * @param $variables
 *   An associative array containing:
456
 *   - node: The node entity which is being previewed.
457
 *
458
 * @see node_preview()
459
 * @ingroup themeable
460
 */
461 462 463
function theme_node_preview($variables) {
  $node = $variables['node'];

464
  $output = '';
465

466
  $preview_trimmed_version = FALSE;
467

468 469 470 471
  $elements = node_view(clone $node, 'teaser');
  $trimmed = drupal_render($elements);
  $elements = node_view($node, 'full');
  $full = drupal_render($elements);
472 473 474

  // Do we need to preview trimmed version of post as well as full version?
  if ($trimmed != $full) {
475
    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>'));
476
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
477
    $output .= $trimmed;
478
    $output .= '<h3>' . t('Preview full version') . '</h3>';
479
    $output .= $full;
480 481
  }
  else {
482
    $output .= $full;
483 484 485 486 487
  }

  return $output;
}

488 489 490 491 492 493 494 495
/**
 * 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()
 */
496
function node_form_submit($form, &$form_state) {
497 498 499
  // Handle possible field translations first and then build the node from the
  // submitted values.
  node_field_language_form_submit($form, $form_state);
500
  $node = node_form_submit_build_node($form, $form_state);
501
  $insert = empty($node->nid);
502
  $node->save();
503
  $node_link = l(t('view'), 'node/' . $node->nid);
504 505
  $watchdog_args = array('@type' => $node->type, '%title' => $node->title);
  $t_args = array('@type' => node_type_get_name($node), '%title' => $node->title);
506 507

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

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
/**
 * Handles possible node language changes.
 *
 */
function node_field_language_form_submit($form, &$form_state) {
  if (field_has_translation_handler('node', 'node')) {
    $bundle = $form_state['values']['type'];
    $node_language = $form_state['values']['langcode'];

    foreach (field_info_instances('node', $bundle) as $instance) {
      $field_name = $instance['field_name'];
      $field = field_info_field($field_name);
      $previous_langcode = $form[$field_name]['#language'];

      // Handle a possible language change: New language values are inserted,
      // previous ones are deleted.
      if ($field['translatable'] && $previous_langcode != $node_language) {
        $form_state['values'][$field_name][$node_language] = $form_state['values'][$field_name][$previous_langcode];
        $form_state['values'][$field_name][$previous_langcode] = array();
      }
    }
  }
}

554
/**
555 556
 * Updates the form state's node entity by processing this submission's values.
 *
557
 * This is the default builder function for the node form. It is called
558 559 560 561 562 563
 * 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()
564 565 566 567
 * @see node_form_delete_submit()
 * @see node_form_build_preview()
 * @see node_form_validate()
 * @see node_form_submit()
568 569
 */
function node_form_submit_build_node($form, &$form_state) {
570 571 572 573 574 575 576
  // @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.
577
  unset($form_state['submit_handlers']);
578 579
  form_execute_handlers('submit', $form, $form_state);

580 581
  $node = $form_state['node'];
  entity_form_submit_build_entity('node', $node, $form, $form_state);
Dries's avatar
 
Dries committed
582

583 584 585 586 587
  node_submit($node);
  foreach (module_implements('node_submit') as $module) {
    $function = $module . '_node_submit';
    $function($node, $form, $form_state);
  }
588 589 590 591
  return $node;
}

/**
592 593 594
 * Page callback: Form constructor for node deletion confirmation form.
 *
 * @see node_menu()
595
 */
596
function node_delete_confirm($form, &$form_state, $node) {
597 598 599
  $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);
600
  return confirm_form($form,
601
    t('Are you sure you want to delete %title?', array('%title' => $node->title)),
602
    'node/' . $node->nid,
603
    t('This action cannot be undone.'),
604
    t('Delete'),
605 606
    t('Cancel')
  );
607 608 609
}

/**
610
 * Form submission handler for node_delete_confirm().
611 612 613
 */
function node_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
614
    $node = node_load($form_state['values']['nid']);
615
    node_delete($form_state['values']['nid']);
616 617
    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)));
618 619 620 621 622 623
  }

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

/**
624 625 626
 * Page callback: Generates an overview table of older revisions of a node.
 *
 * @see node_menu()
627
 */
628
function node_revision_overview($node) {
629
  drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
630 631 632 633 634 635 636 637 638 639 640

  $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;
641
  if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
642 643 644 645 646 647 648
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
    $operations = array();

    if ($revision->current_vid > 0) {
649
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'short'), "node/$node->nid"), '!username' => theme('username', array('account' => $revision))))
650
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
651
                     'class' => array('revision-current'));
652
      $operations[] = array('data' => drupal_placeholder(t('current revision')), 'class' => array('revision-current'), 'colspan' => 2);
653 654
    }
    else {
655
      $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))))
656
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
657 658 659 660 661 662 663 664 665
      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);
  }
666

667 668 669 670
  $build['node_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
671 672 673
    '#attached' => array (
      'css' => array(drupal_get_path('module', 'node') . '/node.admin.css'),
    ),
674
  );
675

676
  return $build;
677 678
}

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

692 693 694
/**
 * Form submission handler for node_revision_revert_confirm().
 */
695 696 697
function node_revision_revert_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
  $node_revision->revision = 1;
698 699 700 701 702 703

  // The revision timestamp will be updated when the revision is saved. Keep the
  // original one for the confirmation message.
  $original_revision_timestamp = $node_revision->revision_timestamp;

  $node_revision->log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp)));
704

705
  $node_revision->save();
706

707
  watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->title, '%revision' => $node_revision->vid));
708
  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($original_revision_timestamp))));
709
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
710
}
711

712 713 714 715 716 717 718 719
/**
 * 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()
 */
720
function node_revision_delete_confirm($form, $form_state, $node_revision) {
721
  $form['#node_revision'] = $node_revision;
722
  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'));
723 724
}

725 726 727
/**
 * Form submission handler for node_revision_delete_confirm().
 */
728 729
function node_revision_delete_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
730 731
  node_revision_delete($node_revision->vid);

732 733
  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)));
734
  $form_state['redirect'] = 'node/' . $node_revision->nid;
735
  if (db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid', array(':nid' => $node_revision->nid))->fetchField() > 1) {
736 737 738
    $form_state['redirect'] .= '/revisions';
  }
}