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
33
  $content = array();
  // Only use node types the user has access to.
  foreach (node_type_get_types() as $type) {
34
    if (node_access('create', $type->type)) {
35
36
37
      $content[$type->type] = $type;
    }
  }
38
39
  // Bypass the node/add listing if only one content type is available.
  if (count($content) == 1) {
40
    $type = array_shift($content);
41
    return new RedirectResponse(url('node/add/' . $type->type, array('absolute' => TRUE)));
42
  }
43
  return array('#theme' => 'node_add_list', '#content' => $content);
44
45
}

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

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


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

91
  $type = $node_type->type;
92
  $langcode = module_invoke('language', 'get_default_langcode', 'node', $type);
93
  $node = entity_create('node', array(
94
    'uid' => $user->id(),
95
    'name' => $user->getUsername(),
96
    'type' => $type,
97
    'langcode' => $langcode ? $langcode : language_default()->id,
98
  ));
99
  drupal_set_title(t('Create @name', array('@name' => $node_type->name)), PASS_THROUGH);
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) {
115
116
  if (node_access('create', $node) || node_access('update', $node)) {

117
    $node->changed = REQUEST_TIME;
118

119
    // Display a preview of the node.
120
    if (!form_get_errors()) {
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
  drupal_set_title(t('Revisions for %title', array('%title' => $node->label())), PASS_THROUGH);
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', $node)) {
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', $node)) {
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
}