node.pages.inc 12.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
use Drupal\node\Plugin\Core\Entity\Node;
13 14

/**
15 16
 * Page callback: Presents the node editing form.
 *
17 18 19 20 21 22
 * @param object $node
 *   A node object.
 *
 * @return array
 *   A form array as expected by drupal_render().
 *
23
 * @see node_menu()
24
 */
25
function node_page_edit($node) {
26
  drupal_set_title(t('<em>Edit @type</em> @title', array('@type' => node_get_type_label($node), '@title' => $node->label())), PASS_THROUGH);
27
  return entity_get_form($node);
28 29
}

30
/**
31 32 33
 * Page callback: Displays add content links for available content types.
 *
 * Redirects to node/add/[type] if only one content type is available.
34
 *
35 36 37 38 39
 * @return array
 *   A render array for a list of the node types that can be added; however, if
 *   there is only one node type defined for the site, the function redirects
 *   to the node add page for that one node type and does not return at all.
 *
40 41
 * @see node_menu()
 */
42
function node_add_page() {
43 44 45 46 47 48 49
  $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;
    }
  }
50 51
  // Bypass the node/add listing if only one content type is available.
  if (count($content) == 1) {
52 53
    $type = array_shift($content);
    drupal_goto('node/add/' . $type->type);
54
  }
55
  return array('#theme' => 'node_add_list', '#content' => $content);
56 57
}

58
/**
59 60 61 62 63
 * Returns HTML for a list of available node types for node creation.
 *
 * @param $variables
 *   An associative array containing:
 *   - content: An array of content types.
64
 *
65
 * @see node_add_page()
66
 *
67 68
 * @ingroup themeable
 */
69 70
function theme_node_add_list($variables) {
  $content = $variables['content'];
71 72 73 74
  $output = '';

  if ($content) {
    $output = '<dl class="node-type-list">';
75 76 77
    foreach ($content as $type) {
      $output .= '<dt>' . l($type->name, 'node/add/' . $type->type) . '</dt>';
      $output .= '<dd>' . filter_xss_admin($type->description) . '</dd>';
78 79 80
    }
    $output .= '</dl>';
  }
81
  else {
82
    $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>';
83
  }
84 85 86 87 88
  return $output;
}


/**
89 90
 * Page callback: Provides the node submission form.
 *
91 92
 * @param $node_type
 *   The node type object for the submitted node.
93
 *
94 95
 * @return array
 *   A node submission form.
96 97
 *
 * @see node_menu()
98
 */
99
function node_add($node_type) {
100 101
  global $user;

102
  $type = $node_type->type;
103
  $langcode = module_invoke('language', 'get_default_langcode', 'node', $type);
104 105 106 107
  $node = entity_create('node', array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $type,
108
    'langcode' => $langcode ? $langcode : language_default()->langcode,
109
  ));
110
  drupal_set_title(t('Create @name', array('@name' => $node_type->name)), PASS_THROUGH);
111
  $output = entity_get_form($node);
112 113 114 115 116

  return $output;
}

/**
117 118
 * Generates a node preview.
 *
119
 * @param Drupal\node\Node $node
120 121 122
 *   The node to preview.
 *
 * @return
123
 *   An HTML-formatted string of a node preview.
124 125
 *
 * @see node_form_build_preview()
126
 */
127
function node_preview(Node $node) {
128
  if (node_access('create', $node) || node_access('update', $node)) {
129
    _field_invoke_multiple('load', 'node', array($node->nid => $node));
130
    // Load the user's name when needed.
131 132 133
    if (isset($node->name)) {
      // The use of isset() is mandatory in the context of user IDs, because
      // user ID 0 denotes the anonymous user.
134
      if ($user = user_load_by_name($node->name)) {
135 136 137 138 139 140
        $node->uid = $user->uid;
      }
      else {
        $node->uid = 0; // anonymous user
      }
    }
141
    elseif ($node->uid) {
142
      $user = user_load($node->uid);
143 144 145
      $node->name = $user->name;
    }

146
    $node->changed = REQUEST_TIME;
147 148
    $nodes = array($node->nid => $node);
    field_attach_prepare_view('node', $nodes, 'full');
149

150
    // Display a preview of the node.
151
    if (!form_get_errors()) {
152 153 154
      $node->in_preview = TRUE;
      $output = theme('node_preview', array('node' => $node));
      unset($node->in_preview);
155 156 157 158 159 160 161
    }

    return $output;
  }
}

/**
162
 * Returns HTML for a node preview for display during node creation and editing.
163
 *
164 165
 * @param $variables
 *   An associative array containing:
166
 *   - node: The node entity which is being previewed.
167
 *
168
 * @see NodeFormController::preview()
169
 * @see node_preview()
170
 *
171
 * @ingroup themeable
172
 */
173 174 175
function theme_node_preview($variables) {
  $node = $variables['node'];

176
  $output = '';
177

178 179 180
  $preview_trimmed_version = FALSE;

  $elements = node_view(clone $node, 'teaser');
181
  $elements['#attached']['library'][] = array('node', 'drupal.node.preview');
182 183 184
  $trimmed = drupal_render($elements);
  $elements = node_view($node, 'full');
  $full = drupal_render($elements);
185 186 187

  // Do we need to preview trimmed version of post as well as full version?
  if ($trimmed != $full) {
188
    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>'));
189
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
190
    $output .= $trimmed;
191
    $output .= '<h3>' . t('Preview full version') . '</h3>';
192
    $output .= $full;
193 194
  }
  else {
195
    $output .= $full;
196 197 198 199 200 201
  }

  return $output;
}

/**
202 203
 * Page callback: Form constructor for node deletion confirmation form.
 *
204 205 206 207 208 209
 * @param object $node
 *   A node object.
 *
 * @return array
 *   A form array.
 *
210
 * @see node_delete_confirm_submit()
211
 * @see node_menu()
212
 */
213
function node_delete_confirm($form, &$form_state, $node) {
214 215
  // Always provide entity id in the same form key as in the entity edit form.
  $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
216
  return confirm_form($form,
217
    t('Are you sure you want to delete %title?', array('%title' => $node->label())),
218
    'node/' . $node->nid,
219
    t('This action cannot be undone.'),
220
    t('Delete'),
221 222
    t('Cancel')
  );
223 224 225
}

/**
226
 * Form submission handler for node_delete_confirm().
227 228 229
 */
function node_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
230
    $node = node_load($form_state['values']['nid']);
231
    node_delete($form_state['values']['nid']);
232
    watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->label()));
233
    drupal_set_message(t('@type %title has been deleted.', array('@type' => node_get_type_label($node), '%title' => $node->label())));
234 235 236 237 238 239
  }

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

/**
240 241
 * Page callback: Generates an overview table of older revisions of a node.
 *
242 243 244 245 246 247
 * @param object $node
 *   A node object.
 *
 * @return array
 *   An array as expected by drupal_render().
 *
248
 * @see node_menu()
249
 */
250
function node_revision_overview($node) {
251
  drupal_set_title(t('Revisions for %title', array('%title' => $node->label())), PASS_THROUGH);
252

253
  $header = array(t('Revision'), t('Operations'));
254 255 256 257

  $revisions = node_revision_list($node);

  $rows = array();
258 259
  $type = $node->type;

260
  $revert_permission = FALSE;
261
  if ((user_access("revert $type revisions") || user_access('revert all revisions') || user_access('administer nodes')) && node_access('update', $node)) {
262 263 264
    $revert_permission = TRUE;
  }
  $delete_permission = FALSE;
265
  if ((user_access("delete $type revisions") || user_access('delete all revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
266 267 268 269
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
270
    $type = $node->type;
271
    if ($revision->current_vid > 0) {
272
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->timestamp, 'short'), "node/$node->nid"), '!username' => theme('username', array('account' => $revision))))
273
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
274
                     'class' => array('revision-current'));
275
      $row[] = array('data' => drupal_placeholder(t('current revision')), 'class' => array('revision-current'));
276 277
    }
    else {
278
      $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))))
279
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
280
      if ($revert_permission) {
281 282 283 284
        $links['revert'] = array(
          'title' => t('revert'),
          'href' => "node/$node->nid/revisions/$revision->vid/revert",
        );
285 286
      }
      if ($delete_permission) {
287 288 289 290
        $links['delete'] = array(
          'title' => t('delete'),
          'href' => "node/$node->nid/revisions/$revision->vid/delete",
        );
291
      }
292 293 294 295 296 297
      $row[] = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => $links,
        ),
      );
298
    }
299
    $rows[] = $row;
300
  }
301

302 303 304 305
  $build['node_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
306 307 308
    '#attached' => array (
      'css' => array(drupal_get_path('module', 'node') . '/node.admin.css'),
    ),
309
  );
310

311
  return $build;
312 313
}

314
/**
315 316 317 318
 * Page callback: Form constructor for the reversion confirmation form.
 *
 * This form prevents against CSRF attacks.
 *
319 320 321 322 323 324
 * @param int $node_revision
 *   The node revision ID.
 *
 * @return array
 *   An array as expected by drupal_render().
 *
325 326
 * @see node_menu()
 * @see node_revision_revert_confirm_submit()
327
 * @ingroup forms
328
 */
329
function node_revision_revert_confirm($form, $form_state, $node_revision) {
330
  $form['#node_revision'] = $node_revision;
331
  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'));
332 333
}

334 335 336
/**
 * Form submission handler for node_revision_revert_confirm().
 */
337 338
function node_revision_revert_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
339
  $node_revision->setNewRevision();
340
  // Make this the new default revision for the node.
341
  $node_revision->isDefaultRevision(TRUE);
342 343 344 345 346 347

  // 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)));
348

349
  $node_revision->save();
350

351
  watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->label(), '%revision' => $node_revision->vid));
352
  drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_get_type_label($node_revision), '%title' => $node_revision->label(), '%revision-date' => format_date($original_revision_timestamp))));
353
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
354
}
355

356 357 358 359 360
/**
 * Page callback: Form constructor for the revision deletion confirmation form.
 *
 * This form prevents against CSRF attacks.
 *
361 362 363 364 365 366
 * @param $node_revision
 *   The node revision ID.
 *
 * @return
 *   An array as expected by drupal_render().
 *
367 368
 * @see node_menu()
 * @see node_revision_delete_confirm_submit()
369
 * @ingroup forms
370
 */
371
function node_revision_delete_confirm($form, $form_state, $node_revision) {
372
  $form['#node_revision'] = $node_revision;
373
  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'));
374 375
}

376 377 378
/**
 * Form submission handler for node_revision_delete_confirm().
 */
379 380
function node_revision_delete_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
381 382
  node_revision_delete($node_revision->vid);

383
  watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->label(), '%revision' => $node_revision->vid));
384
  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_type_label($node_revision), '%title' => $node_revision->label())));
385
  $form_state['redirect'] = 'node/' . $node_revision->nid;
386
  if (db_query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid', array(':nid' => $node_revision->nid))->fetchField() > 1) {
387 388 389
    $form_state['redirect'] .= '/revisions';
  }
}