node.pages.inc 7.81 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
use Drupal\node\NodeInterface;
15

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

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

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


/**
77 78
 * Page callback: Provides the node submission form.
 *
79 80
 * @param $node_type
 *   The node type object for the submitted node.
81
 *
82 83
 * @return array
 *   A node submission form.
84 85
 *
 * @see node_menu()
86 87
 *
 * @deprecated Use \Drupal\node\Controller\NodeController::add()
88
 */
89
function node_add($node_type) {
90
  $user = \Drupal::currentUser();
91

92
  $type = $node_type->type;
93
  $langcode = module_invoke('language', 'get_default_langcode', 'node', $type);
94
  $node = entity_create('node', array(
95
    'uid' => $user->id(),
96
    'name' => $user->getUsername(),
97
    'type' => $type,
98
    'langcode' => $langcode ? $langcode : language_default()->id,
99
  ));
100
  return \Drupal::entityManager()->getForm($node);
101 102 103
}

/**
104 105
 * Generates a node preview.
 *
106
 * @param \Drupal\Core\Entity\EntityInterface $node
107 108 109
 *   The node to preview.
 *
 * @return
110
 *   An HTML-formatted string of a node preview.
111 112
 *
 * @see node_form_build_preview()
113
 */
114
function node_preview(NodeInterface $node, array &$form_state) {
115
  if ($node->access('create') || $node->access('update')) {
116

117
    $node->changed = REQUEST_TIME;
118

119
    // Display a preview of the node.
120
    if (!form_get_errors($form_state)) {
121
      $node->in_preview = TRUE;
122 123 124 125 126
      $node_preview = array(
        '#theme' => 'node_preview',
        '#node' => $node,
      );
      $output = drupal_render($node_preview);
127
      unset($node->in_preview);
128 129 130 131 132 133 134
    }

    return $output;
  }
}

/**
135
 * Returns HTML for a node preview for display during node creation and editing.
136
 *
137 138
 * @param $variables
 *   An associative array containing:
139
 *   - node: The node entity which is being previewed.
140
 *
141
 * @see NodeFormController::preview()
142
 * @see node_preview()
143
 *
144
 * @ingroup themeable
145
 */
146 147 148
function theme_node_preview($variables) {
  $node = $variables['node'];

149
  $output = '';
150

151
  $elements = node_view($node, 'teaser');
152
  $elements['#attached']['library'][] = array('node', 'drupal.node.preview');
153 154 155
  $trimmed = drupal_render($elements);
  $elements = node_view($node, 'full');
  $full = drupal_render($elements);
156 157 158

  // Do we need to preview trimmed version of post as well as full version?
  if ($trimmed != $full) {
159
    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>'));
160
    $output .= '<h3>' . t('Preview trimmed version') . '</h3>';
161
    $output .= $trimmed;
162
    $output .= '<h3>' . t('Preview full version') . '</h3>';
163
    $output .= $full;
164 165
  }
  else {
166
    $output .= $full;
167 168 169 170 171 172
  }

  return $output;
}

/**
173 174
 * Page callback: Generates an overview table of older revisions of a node.
 *
175 176 177 178 179 180
 * @param object $node
 *   A node object.
 *
 * @return array
 *   An array as expected by drupal_render().
 *
181
 * @see node_menu()
182 183
 *
 * @deprecated Use \Drupal\node\Controller\NodeController::revisionOverview()
184
 */
185
function node_revision_overview($node) {
186
  $build['#title'] = t('Revisions for %title', array('%title' => $node->label()));
187

188
  $header = array(t('Revision'), t('Operations'));
189 190 191 192

  $revisions = node_revision_list($node);

  $rows = array();
193
  $type = $node->getType();
194

195
  $revert_permission = FALSE;
196
  if ((user_access("revert $type revisions") || user_access('revert all revisions') || user_access('administer nodes')) && $node->access('update')) {
197 198 199
    $revert_permission = TRUE;
  }
  $delete_permission = FALSE;
200
  if ((user_access("delete $type revisions") || user_access('delete all revisions') || user_access('administer nodes')) && $node->access('delete')) {
201 202 203 204 205
    $delete_permission = TRUE;
  }
  foreach ($revisions as $revision) {
    $row = array();
    if ($revision->current_vid > 0) {
206 207
      $username = array(
        '#theme' => 'username',
208
        '#account' => user_load($revision->uid),
209
      );
210
      $row[] = array('data' => t('!date by !username', array('!date' => l(format_date($revision->revision_timestamp, 'short'), 'node/' . $node->id()), '!username' => drupal_render($username)))
211
                               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
212
                     'class' => array('revision-current'));
213
      $row[] = array('data' => drupal_placeholder(t('current revision')), 'class' => array('revision-current'));
214 215
    }
    else {
216 217
      $username = array(
        '#theme' => 'username',
218
        '#account' => user_load($revision->uid),
219
      );
220
      $row[] = t('!date by !username', array('!date' => l(format_date($revision->revision_timestamp, 'short'), "node/" . $node->id() . "/revisions/" . $revision->vid . "/view"), '!username' => drupal_render($username)))
221
               . (($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '');
222
      if ($revert_permission) {
223
        $links['revert'] = array(
224
          'title' => t('Revert'),
225
          'href' => "node/" . $node->id() . "/revisions/" . $revision->vid . "/revert",
226
        );
227 228
      }
      if ($delete_permission) {
229
        $links['delete'] = array(
230
          'title' => t('Delete'),
231
          'href' => "node/" . $node->id() . "/revisions/" . $revision->vid . "/delete",
232
        );
233
      }
234 235 236 237 238 239
      $row[] = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => $links,
        ),
      );
240
    }
241
    $rows[] = $row;
242
  }
243

244 245 246 247
  $build['node_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
248
    '#attached' => array (
249
      'css' => array(drupal_get_path('module', 'node') . '/css/node.admin.css'),
250
    ),
251
  );
252

253
  return $build;
254
}