node.pages.inc 12.7 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\Core\Entity\EntityInterface;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14

15
/**
16 17 18
 * Page callback: Displays add content links for available content types.
 *
 * Redirects to node/add/[type] if only one content type is available.
19
 *
20 21 22 23 24
 * @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.
 *
25 26
 * @see node_menu()
 */
27
function node_add_page() {
28 29 30
  $content = array();
  // Only use node types the user has access to.
  foreach (node_type_get_types() as $type) {
31
    if (node_access('create', $type->type)) {
32 33 34
      $content[$type->type] = $type;
    }
  }
35 36
  // Bypass the node/add listing if only one content type is available.
  if (count($content) == 1) {
37
    $type = array_shift($content);
38
    return new RedirectResponse(url('node/add/' . $type->type, array('absolute' => TRUE)));
39
  }
40
  return array('#theme' => 'node_add_list', '#content' => $content);
41 42
}

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

  if ($content) {
    $output = '<dl class="node-type-list">';
60 61 62
    foreach ($content as $type) {
      $output .= '<dt>' . l($type->name, 'node/add/' . $type->type) . '</dt>';
      $output .= '<dd>' . filter_xss_admin($type->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
 * Page callback: Provides the node submission form.
 *
76 77
 * @param $node_type
 *   The node type object for the submitted node.
78
 *
79 80
 * @return array
 *   A node submission form.
81 82
 *
 * @see node_menu()
83
 */
84
function node_add($node_type) {
85 86
  global $user;

87
  $type = $node_type->type;
88
  $langcode = module_invoke('language', 'get_default_langcode', 'node', $type);
89 90 91 92
  $node = entity_create('node', array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $type,
93
    'langcode' => $langcode ? $langcode : language_default()->langcode,
94
  ))->getBCEntity();
95
  drupal_set_title(t('Create @name', array('@name' => $node_type->name)), PASS_THROUGH);
96
  return Drupal::entityManager()->getForm($node);
97 98 99
}

/**
100 101
 * Generates a node preview.
 *
102
 * @param \Drupal\Core\Entity\EntityInterface $node
103 104 105
 *   The node to preview.
 *
 * @return
106
 *   An HTML-formatted string of a node preview.
107 108
 *
 * @see node_form_build_preview()
109
 */
110
function node_preview(EntityInterface $node) {
111
  if (node_access('create', $node) || node_access('update', $node)) {
112
    _field_invoke_multiple('load', 'node', array($node->nid => $node));
113
    // Load the user's name when needed.
114 115 116
    if (isset($node->name)) {
      // The use of isset() is mandatory in the context of user IDs, because
      // user ID 0 denotes the anonymous user.
117
      if ($user = user_load_by_name($node->name)) {
118 119 120 121 122 123
        $node->uid = $user->uid;
      }
      else {
        $node->uid = 0; // anonymous user
      }
    }
124
    elseif ($node->uid) {
125
      $user = user_load($node->uid);
126 127 128
      $node->name = $user->name;
    }

129
    $node->changed = REQUEST_TIME;
130

131
    // Display a preview of the node.
132
    if (!form_get_errors()) {
133
      $node->in_preview = TRUE;
134 135 136 137 138
      $node_preview = array(
        '#theme' => 'node_preview',
        '#node' => $node,
      );
      $output = drupal_render($node_preview);
139
      unset($node->in_preview);
140 141 142 143 144 145 146
    }

    return $output;
  }
}

/**
147
 * Returns HTML for a node preview for display during node creation and editing.
148
 *
149 150
 * @param $variables
 *   An associative array containing:
151
 *   - node: The node entity which is being previewed.
152
 *
153
 * @see NodeFormController::preview()
154
 * @see node_preview()
155
 *
156
 * @ingroup themeable
157
 */
158 159 160
function theme_node_preview($variables) {
  $node = $variables['node'];

161
  $output = '';
162

163
  $elements = node_view($node, 'teaser');
164
  $elements['#attached']['library'][] = array('node', 'drupal.node.preview');
165 166 167
  $trimmed = drupal_render($elements);
  $elements = node_view($node, 'full');
  $full = drupal_render($elements);
168 169 170

  // Do we need to preview trimmed version of post as well as full version?
  if ($trimmed != $full) {
171
    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>'));
172
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
173
    $output .= $trimmed;
174
    $output .= '<h3>' . t('Preview full version') . '</h3>';
175
    $output .= $full;
176 177
  }
  else {
178
    $output .= $full;
179 180 181 182 183 184
  }

  return $output;
}

/**
185 186
 * Page callback: Form constructor for node deletion confirmation form.
 *
187 188 189 190 191 192
 * @param object $node
 *   A node object.
 *
 * @return array
 *   A form array.
 *
193
 * @see node_delete_confirm_submit()
194
 * @see node_menu()
195
 */
196
function node_delete_confirm($form, &$form_state, $node) {
197 198
  // Always provide entity id in the same form key as in the entity edit form.
  $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
199
  return confirm_form($form,
200
    t('Are you sure you want to delete %title?', array('%title' => $node->label())),
201
    'node/' . $node->nid,
202
    t('This action cannot be undone.'),
203
    t('Delete'),
204 205
    t('Cancel')
  );
206 207 208
}

/**
209
 * Form submission handler for node_delete_confirm().
210 211 212
 */
function node_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
213
    $node = node_load($form_state['values']['nid']);
214
    $node->delete();
215
    watchdog('content', '@type: deleted %title.', array('@type' => $node->type, '%title' => $node->label()));
216
    drupal_set_message(t('@type %title has been deleted.', array('@type' => node_get_type_label($node), '%title' => $node->label())));
217 218 219 220 221 222
  }

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

/**
223 224
 * Page callback: Generates an overview table of older revisions of a node.
 *
225 226 227 228 229 230
 * @param object $node
 *   A node object.
 *
 * @return array
 *   An array as expected by drupal_render().
 *
231
 * @see node_menu()
232
 */
233
function node_revision_overview($node) {
234
  drupal_set_title(t('Revisions for %title', array('%title' => $node->label())), PASS_THROUGH);
235

236
  $header = array(t('Revision'), t('Operations'));
237 238 239 240

  $revisions = node_revision_list($node);

  $rows = array();
241 242
  $type = $node->type;

243
  $revert_permission = FALSE;
244
  if ((user_access("revert $type revisions") || user_access('revert all revisions') || user_access('administer nodes')) && node_access('update', $node)) {
245 246 247
    $revert_permission = TRUE;
  }
  $delete_permission = FALSE;
248
  if ((user_access("delete $type revisions") || user_access('delete all revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
249 250 251 252
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
253
    $type = $node->type;
254
    if ($revision->current_vid > 0) {
255 256 257 258 259
      $username = array(
        '#theme' => 'username',
        '#account' => $revision,
      );
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->revision_timestamp, 'short'), "node/$node->nid"), '!username' => drupal_render($username)))
260
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
261
                     'class' => array('revision-current'));
262
      $row[] = array('data' => drupal_placeholder(t('current revision')), 'class' => array('revision-current'));
263 264
    }
    else {
265 266 267 268 269
      $username = array(
        '#theme' => 'username',
        '#account' => $revision,
      );
      $row[] = t('!date by !username', array('!date' => l(format_date($revision->revision_timestamp, 'short'), "node/$node->nid/revisions/$revision->vid/view"), '!username' => drupal_render($username)))
270
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
271
      if ($revert_permission) {
272
        $links['revert'] = array(
273
          'title' => t('Revert'),
274 275
          'href' => "node/$node->nid/revisions/$revision->vid/revert",
        );
276 277
      }
      if ($delete_permission) {
278
        $links['delete'] = array(
279
          'title' => t('Delete'),
280 281
          'href' => "node/$node->nid/revisions/$revision->vid/delete",
        );
282
      }
283 284 285 286 287 288
      $row[] = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => $links,
        ),
      );
289
    }
290
    $rows[] = $row;
291
  }
292

293 294 295 296
  $build['node_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
297
    '#attached' => array (
298
      'css' => array(drupal_get_path('module', 'node') . '/css/node.admin.css'),
299
    ),
300
  );
301

302
  return $build;
303 304
}

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

325 326 327
/**
 * Form submission handler for node_revision_revert_confirm().
 */
328 329
function node_revision_revert_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
330
  $node_revision->setNewRevision();
331
  // Make this the new default revision for the node.
332
  $node_revision->isDefaultRevision(TRUE);
333 334 335 336 337 338

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

340
  $node_revision->save();
341

342
  watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->label(), '%revision' => $node_revision->vid));
343
  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))));
344
  $form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
345
}
346

347 348 349 350 351
/**
 * Page callback: Form constructor for the revision deletion confirmation form.
 *
 * This form prevents against CSRF attacks.
 *
352 353 354 355 356 357
 * @param $node_revision
 *   The node revision ID.
 *
 * @return
 *   An array as expected by drupal_render().
 *
358 359
 * @see node_menu()
 * @see node_revision_delete_confirm_submit()
360
 * @ingroup forms
361
 */
362
function node_revision_delete_confirm($form, $form_state, $node_revision) {
363
  $form['#node_revision'] = $node_revision;
364
  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'));
365 366
}

367 368 369
/**
 * Form submission handler for node_revision_delete_confirm().
 */
370 371
function node_revision_delete_confirm_submit($form, &$form_state) {
  $node_revision = $form['#node_revision'];
372 373
  node_revision_delete($node_revision->vid);

374
  watchdog('content', '@type: deleted %title revision %revision.', array('@type' => $node_revision->type, '%title' => $node_revision->label(), '%revision' => $node_revision->vid));
375
  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())));
376
  $form_state['redirect'] = 'node/' . $node_revision->nid;
377
  if (db_query('SELECT COUNT(DISTINCT vid) FROM {node_field_revision} WHERE nid = :nid', array(':nid' => $node_revision->nid))->fetchField() > 1) {
378 379 380
    $form_state['redirect'] .= '/revisions';
  }
}