Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * The core module that allows content to be submitted to the site.
|
Chris@0
|
6 *
|
Chris@0
|
7 * Modules and scripts may programmatically submit nodes using the usual form
|
Chris@0
|
8 * API pattern.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@18
|
11 use Drupal\Component\Utility\Environment;
|
Chris@0
|
12 use Drupal\Component\Utility\Xss;
|
Chris@0
|
13 use Drupal\Core\Access\AccessResult;
|
Chris@0
|
14 use Drupal\Core\Cache\Cache;
|
Chris@0
|
15 use Drupal\Core\Database\Query\AlterableInterface;
|
Chris@0
|
16 use Drupal\Core\Database\Query\SelectInterface;
|
Chris@0
|
17 use Drupal\Core\Database\StatementInterface;
|
Chris@0
|
18 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
Chris@0
|
19 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
20 use Drupal\Core\Render\Element;
|
Chris@0
|
21 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
22 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
23 use Drupal\Core\Template\Attribute;
|
Chris@0
|
24 use Drupal\Core\Url;
|
Chris@0
|
25 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
26 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
27 use Drupal\language\ConfigurableLanguageInterface;
|
Chris@0
|
28 use Drupal\node\Entity\Node;
|
Chris@0
|
29 use Drupal\node\Entity\NodeType;
|
Chris@0
|
30 use Drupal\node\NodeInterface;
|
Chris@0
|
31 use Drupal\node\NodeTypeInterface;
|
Chris@17
|
32 use Drupal\user\UserInterface;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Denotes that the node is not published.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @deprecated Scheduled for removal in Drupal 9.0.x.
|
Chris@0
|
38 * Use \Drupal\node\NodeInterface::NOT_PUBLISHED instead.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @see https://www.drupal.org/node/2316145
|
Chris@0
|
41 */
|
Chris@0
|
42 const NODE_NOT_PUBLISHED = 0;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Denotes that the node is published.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @deprecated Scheduled for removal in Drupal 9.0.x.
|
Chris@0
|
48 * Use \Drupal\node\NodeInterface::PUBLISHED instead.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @see https://www.drupal.org/node/2316145
|
Chris@0
|
51 */
|
Chris@0
|
52 const NODE_PUBLISHED = 1;
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Denotes that the node is not promoted to the front page.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @deprecated Scheduled for removal in Drupal 9.0.x.
|
Chris@0
|
58 * Use \Drupal\node\NodeInterface::NOT_PROMOTED instead.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @see https://www.drupal.org/node/2316145
|
Chris@0
|
61 */
|
Chris@0
|
62 const NODE_NOT_PROMOTED = 0;
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Denotes that the node is promoted to the front page.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @deprecated Scheduled for removal in Drupal 9.0.x.
|
Chris@0
|
68 * Use \Drupal\node\NodeInterface::PROMOTED instead.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @see https://www.drupal.org/node/2316145
|
Chris@0
|
71 */
|
Chris@0
|
72 const NODE_PROMOTED = 1;
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Denotes that the node is not sticky at the top of the page.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @deprecated Scheduled for removal in Drupal 9.0.x.
|
Chris@0
|
78 * Use \Drupal\node\NodeInterface::NOT_STICKY instead.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @see https://www.drupal.org/node/2316145
|
Chris@0
|
81 */
|
Chris@0
|
82 const NODE_NOT_STICKY = 0;
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Denotes that the node is sticky at the top of the page.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @deprecated Scheduled for removal in Drupal 9.0.x.
|
Chris@0
|
88 * Use \Drupal\node\NodeInterface::STICKY instead.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @see https://www.drupal.org/node/2316145
|
Chris@0
|
91 */
|
Chris@0
|
92 const NODE_STICKY = 1;
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Implements hook_help().
|
Chris@0
|
96 */
|
Chris@0
|
97 function node_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
98 // Remind site administrators about the {node_access} table being flagged
|
Chris@0
|
99 // for rebuild. We don't need to issue the message on the confirm form, or
|
Chris@0
|
100 // while the rebuild is being processed.
|
Chris@0
|
101 if ($route_name != 'node.configure_rebuild_confirm' && $route_name != 'system.batch_page.normal' && $route_name != 'help.page.node' && $route_name != 'help.main'
|
Chris@0
|
102 && \Drupal::currentUser()->hasPermission('access administration pages') && node_access_needs_rebuild()) {
|
Chris@0
|
103 if ($route_name == 'system.status') {
|
Chris@0
|
104 $message = t('The content access permissions need to be rebuilt.');
|
Chris@0
|
105 }
|
Chris@0
|
106 else {
|
Chris@18
|
107 $message = t('The content access permissions need to be rebuilt. <a href=":node_access_rebuild">Rebuild permissions</a>.', [':node_access_rebuild' => Url::fromRoute('node.configure_rebuild_confirm')->toString()]);
|
Chris@0
|
108 }
|
Chris@17
|
109 \Drupal::messenger()->addError($message);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 switch ($route_name) {
|
Chris@0
|
113 case 'help.page.node':
|
Chris@0
|
114 $output = '';
|
Chris@0
|
115 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@18
|
116 $output .= '<p>' . t('The Node module manages the creation, editing, deletion, settings, and display of the main site content. Content items managed by the Node module are typically displayed as pages on your site, and include a title, some meta-data (author, creation time, content type, etc.), and optional fields containing text or other data (fields are managed by the <a href=":field">Field module</a>). For more information, see the <a href=":node">online documentation for the Node module</a>.', [':node' => 'https://www.drupal.org/documentation/modules/node', ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '</p>';
|
Chris@0
|
117 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
118 $output .= '<dl>';
|
Chris@0
|
119 $output .= '<dt>' . t('Creating content') . '</dt>';
|
Chris@18
|
120 $output .= '<dd>' . t('When new content is created, the Node module records basic information about the content, including the author, date of creation, and the <a href=":content-type">Content type</a>. It also manages the <em>publishing options</em>, which define whether or not the content is published, promoted to the front page of the site, and/or sticky at the top of content lists. Default settings can be configured for each <a href=":content-type">type of content</a> on your site.', [':content-type' => Url::fromRoute('entity.node_type.collection')->toString()]) . '</dd>';
|
Chris@0
|
121 $output .= '<dt>' . t('Creating custom content types') . '</dt>';
|
Chris@18
|
122 $output .= '<dd>' . t('The Node module gives users with the <em>Administer content types</em> permission the ability to <a href=":content-new">create new content types</a> in addition to the default ones already configured. Creating custom content types gives you the flexibility to add <a href=":field">fields</a> and configure default settings that suit the differing needs of various site content.', [':content-new' => Url::fromRoute('node.type_add')->toString(), ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '</dd>';
|
Chris@0
|
123 $output .= '<dt>' . t('Administering content') . '</dt>';
|
Chris@18
|
124 $output .= '<dd>' . t('The <a href=":content">Content</a> page lists your content, allowing you add new content, filter, edit or delete existing content, or perform bulk operations on existing content.', [':content' => Url::fromRoute('system.admin_content')->toString()]) . '</dd>';
|
Chris@0
|
125 $output .= '<dt>' . t('Creating revisions') . '</dt>';
|
Chris@0
|
126 $output .= '<dd>' . t('The Node module also enables you to create multiple versions of any content, and revert to older versions using the <em>Revision information</em> settings.') . '</dd>';
|
Chris@0
|
127 $output .= '<dt>' . t('User permissions') . '</dt>';
|
Chris@18
|
128 $output .= '<dd>' . t('The Node module makes a number of permissions available for each content type, which can be set by role on the <a href=":permissions">permissions page</a>.', [':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-node'])->toString()]) . '</dd>';
|
Chris@0
|
129 $output .= '</dl>';
|
Chris@0
|
130 return $output;
|
Chris@0
|
131
|
Chris@0
|
132 case 'node.type_add':
|
Chris@0
|
133 return '<p>' . t('Individual content types can have different fields, behaviors, and permissions assigned to them.') . '</p>';
|
Chris@0
|
134
|
Chris@0
|
135 case 'entity.entity_form_display.node.default':
|
Chris@0
|
136 case 'entity.entity_form_display.node.form_mode':
|
Chris@0
|
137 $type = $route_match->getParameter('node_type');
|
Chris@12
|
138 return '<p>' . t('Content items can be edited using different form modes. Here, you can define which fields are shown and hidden when %type content is edited in each form mode, and define how the field form widgets are displayed in each form mode.', ['%type' => $type->label()]) . '</p>';
|
Chris@0
|
139
|
Chris@0
|
140 case 'entity.entity_view_display.node.default':
|
Chris@0
|
141 case 'entity.entity_view_display.node.view_mode':
|
Chris@0
|
142 $type = $route_match->getParameter('node_type');
|
Chris@0
|
143 return '<p>' . t('Content items can be displayed using different view modes: Teaser, Full content, Print, RSS, etc. <em>Teaser</em> is a short format that is typically used in lists of multiple content items. <em>Full content</em> is typically used when the content is displayed on its own page.') . '</p>' .
|
Chris@0
|
144 '<p>' . t('Here, you can define which fields are shown and hidden when %type content is displayed in each view mode, and define how the fields are displayed in each view mode.', ['%type' => $type->label()]) . '</p>';
|
Chris@0
|
145
|
Chris@0
|
146 case 'entity.node.version_history':
|
Chris@0
|
147 return '<p>' . t('Revisions allow you to track differences between multiple versions of your content, and revert to older versions.') . '</p>';
|
Chris@0
|
148
|
Chris@0
|
149 case 'entity.node.edit_form':
|
Chris@0
|
150 $node = $route_match->getParameter('node');
|
Chris@0
|
151 $type = NodeType::load($node->getType());
|
Chris@0
|
152 $help = $type->getHelp();
|
Chris@0
|
153 return (!empty($help) ? Xss::filterAdmin($help) : '');
|
Chris@0
|
154
|
Chris@0
|
155 case 'node.add':
|
Chris@0
|
156 $type = $route_match->getParameter('node_type');
|
Chris@0
|
157 $help = $type->getHelp();
|
Chris@0
|
158 return (!empty($help) ? Xss::filterAdmin($help) : '');
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * Implements hook_theme().
|
Chris@0
|
164 */
|
Chris@0
|
165 function node_theme() {
|
Chris@0
|
166 return [
|
Chris@0
|
167 'node' => [
|
Chris@0
|
168 'render element' => 'elements',
|
Chris@0
|
169 ],
|
Chris@0
|
170 'node_add_list' => [
|
Chris@0
|
171 'variables' => ['content' => NULL],
|
Chris@0
|
172 ],
|
Chris@0
|
173 'node_edit_form' => [
|
Chris@0
|
174 'render element' => 'form',
|
Chris@0
|
175 ],
|
Chris@0
|
176 'field__node__title' => [
|
Chris@0
|
177 'base hook' => 'field',
|
Chris@0
|
178 ],
|
Chris@0
|
179 'field__node__uid' => [
|
Chris@0
|
180 'base hook' => 'field',
|
Chris@0
|
181 ],
|
Chris@0
|
182 'field__node__created' => [
|
Chris@0
|
183 'base hook' => 'field',
|
Chris@0
|
184 ],
|
Chris@0
|
185 ];
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Implements hook_entity_view_display_alter().
|
Chris@0
|
190 */
|
Chris@0
|
191 function node_entity_view_display_alter(EntityViewDisplayInterface $display, $context) {
|
Chris@0
|
192 if ($context['entity_type'] == 'node') {
|
Chris@0
|
193 // Hide field labels in search index.
|
Chris@0
|
194 if ($context['view_mode'] == 'search_index') {
|
Chris@0
|
195 foreach ($display->getComponents() as $name => $options) {
|
Chris@0
|
196 if (isset($options['label'])) {
|
Chris@0
|
197 $options['label'] = 'hidden';
|
Chris@0
|
198 $display->setComponent($name, $options);
|
Chris@0
|
199 }
|
Chris@0
|
200 }
|
Chris@0
|
201 }
|
Chris@0
|
202 }
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 /**
|
Chris@0
|
206 * Gathers a listing of links to nodes.
|
Chris@0
|
207 *
|
Chris@0
|
208 * @param \Drupal\Core\Database\StatementInterface $result
|
Chris@0
|
209 * A database result object from a query to fetch node entities. If your
|
Chris@0
|
210 * query joins the {comment_entity_statistics} table so that the comment_count
|
Chris@0
|
211 * field is available, a title attribute will be added to show the number of
|
Chris@0
|
212 * comments.
|
Chris@0
|
213 * @param $title
|
Chris@0
|
214 * (optional) A heading for the resulting list.
|
Chris@0
|
215 *
|
Chris@0
|
216 * @return array|false
|
Chris@0
|
217 * A renderable array containing a list of linked node titles fetched from
|
Chris@0
|
218 * $result, or FALSE if there are no rows in $result.
|
Chris@0
|
219 */
|
Chris@0
|
220 function node_title_list(StatementInterface $result, $title = NULL) {
|
Chris@0
|
221 $items = [];
|
Chris@0
|
222 $num_rows = FALSE;
|
Chris@0
|
223 $nids = [];
|
Chris@0
|
224 foreach ($result as $row) {
|
Chris@18
|
225 // Do not use $node->label() or $node->toUrl() here, because we only have
|
Chris@0
|
226 // database rows, not actual nodes.
|
Chris@0
|
227 $nids[] = $row->nid;
|
Chris@0
|
228 $options = !empty($row->comment_count) ? ['attributes' => ['title' => \Drupal::translation()->formatPlural($row->comment_count, '1 comment', '@count comments')]] : [];
|
Chris@0
|
229 $items[] = \Drupal::l($row->title, new Url('entity.node.canonical', ['node' => $row->nid], $options));
|
Chris@0
|
230 $num_rows = TRUE;
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 return $num_rows ? ['#theme' => 'item_list__node', '#items' => $items, '#title' => $title, '#cache' => ['tags' => Cache::mergeTags(['node_list'], Cache::buildTags('node', $nids))]] : FALSE;
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * Determines the type of marker to be displayed for a given node.
|
Chris@0
|
238 *
|
Chris@0
|
239 * @param int $nid
|
Chris@0
|
240 * Node ID whose history supplies the "last viewed" timestamp.
|
Chris@0
|
241 * @param int $timestamp
|
Chris@0
|
242 * Time which is compared against node's "last viewed" timestamp.
|
Chris@0
|
243 *
|
Chris@0
|
244 * @return int
|
Chris@0
|
245 * One of the MARK constants.
|
Chris@0
|
246 */
|
Chris@0
|
247 function node_mark($nid, $timestamp) {
|
Chris@0
|
248
|
Chris@0
|
249 $cache = &drupal_static(__FUNCTION__, []);
|
Chris@0
|
250
|
Chris@0
|
251 if (\Drupal::currentUser()->isAnonymous() || !\Drupal::moduleHandler()->moduleExists('history')) {
|
Chris@0
|
252 return MARK_READ;
|
Chris@0
|
253 }
|
Chris@0
|
254 if (!isset($cache[$nid])) {
|
Chris@0
|
255 $cache[$nid] = history_read($nid);
|
Chris@0
|
256 }
|
Chris@0
|
257 if ($cache[$nid] == 0 && $timestamp > HISTORY_READ_LIMIT) {
|
Chris@0
|
258 return MARK_NEW;
|
Chris@0
|
259 }
|
Chris@0
|
260 elseif ($timestamp > $cache[$nid] && $timestamp > HISTORY_READ_LIMIT) {
|
Chris@0
|
261 return MARK_UPDATED;
|
Chris@0
|
262 }
|
Chris@0
|
263 return MARK_READ;
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * Returns a list of all the available node types.
|
Chris@0
|
268 *
|
Chris@0
|
269 * This list can include types that are queued for addition or deletion.
|
Chris@0
|
270 *
|
Chris@0
|
271 * @return \Drupal\node\NodeTypeInterface[]
|
Chris@0
|
272 * An array of node type entities, keyed by ID.
|
Chris@0
|
273 *
|
Chris@0
|
274 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
|
Chris@0
|
275 * Use \Drupal\node\Entity\NodeType::loadMultiple().
|
Chris@0
|
276 *
|
Chris@0
|
277 * @see \Drupal\node\Entity\NodeType::load()
|
Chris@0
|
278 */
|
Chris@0
|
279 function node_type_get_types() {
|
Chris@0
|
280 return NodeType::loadMultiple();
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * Returns a list of available node type names.
|
Chris@0
|
285 *
|
Chris@0
|
286 * This list can include types that are queued for addition or deletion.
|
Chris@0
|
287 *
|
Chris@0
|
288 * @return string[]
|
Chris@0
|
289 * An array of node type labels, keyed by the node type name.
|
Chris@0
|
290 */
|
Chris@0
|
291 function node_type_get_names() {
|
Chris@0
|
292 return array_map(function ($bundle_info) {
|
Chris@0
|
293 return $bundle_info['label'];
|
Chris@18
|
294 }, \Drupal::service('entity_type.bundle.info')->getBundleInfo('node'));
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Returns the node type label for the passed node.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
301 * A node entity to return the node type's label for.
|
Chris@0
|
302 *
|
Chris@0
|
303 * @return string|false
|
Chris@0
|
304 * The node type label or FALSE if the node type is not found.
|
Chris@0
|
305 *
|
Chris@0
|
306 * @todo Add this as generic helper method for config entities representing
|
Chris@0
|
307 * entity bundles.
|
Chris@0
|
308 */
|
Chris@0
|
309 function node_get_type_label(NodeInterface $node) {
|
Chris@0
|
310 $type = NodeType::load($node->bundle());
|
Chris@0
|
311 return $type ? $type->label() : FALSE;
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 /**
|
Chris@0
|
315 * Description callback: Returns the node type description.
|
Chris@0
|
316 *
|
Chris@0
|
317 * @param \Drupal\node\NodeTypeInterface $node_type
|
Chris@0
|
318 * The node type object.
|
Chris@0
|
319 *
|
Chris@0
|
320 * @return string
|
Chris@0
|
321 * The node type description.
|
Chris@0
|
322 */
|
Chris@0
|
323 function node_type_get_description(NodeTypeInterface $node_type) {
|
Chris@0
|
324 return $node_type->getDescription();
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 /**
|
Chris@0
|
328 * Menu argument loader: Loads a node type by string.
|
Chris@0
|
329 *
|
Chris@0
|
330 * @param $name
|
Chris@0
|
331 * The machine name of a node type to load.
|
Chris@0
|
332 *
|
Chris@0
|
333 * @return \Drupal\node\NodeTypeInterface
|
Chris@0
|
334 * A node type object or NULL if $name does not exist.
|
Chris@0
|
335 *
|
Chris@18
|
336 * @deprecated iin Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use
|
Chris@18
|
337 * \Drupal\node\Entity\NodeType::load().
|
Chris@18
|
338 *
|
Chris@18
|
339 * @see https://www.drupal.org/node/2266845
|
Chris@0
|
340 */
|
Chris@0
|
341 function node_type_load($name) {
|
Chris@18
|
342 @trigger_error('node_type_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\node\Entity\NodeType::load(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
|
Chris@0
|
343 return NodeType::load($name);
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 /**
|
Chris@0
|
347 * Adds the default body field to a node type.
|
Chris@0
|
348 *
|
Chris@0
|
349 * @param \Drupal\node\NodeTypeInterface $type
|
Chris@0
|
350 * A node type object.
|
Chris@0
|
351 * @param string $label
|
Chris@0
|
352 * (optional) The label for the body instance.
|
Chris@0
|
353 *
|
Chris@0
|
354 * @return \Drupal\field\Entity\FieldConfig
|
Chris@0
|
355 * A Body field object.
|
Chris@0
|
356 */
|
Chris@0
|
357 function node_add_body_field(NodeTypeInterface $type, $label = 'Body') {
|
Chris@0
|
358 // Add or remove the body field, as needed.
|
Chris@0
|
359 $field_storage = FieldStorageConfig::loadByName('node', 'body');
|
Chris@0
|
360 $field = FieldConfig::loadByName('node', $type->id(), 'body');
|
Chris@0
|
361 if (empty($field)) {
|
Chris@0
|
362 $field = FieldConfig::create([
|
Chris@0
|
363 'field_storage' => $field_storage,
|
Chris@0
|
364 'bundle' => $type->id(),
|
Chris@0
|
365 'label' => $label,
|
Chris@0
|
366 'settings' => ['display_summary' => TRUE],
|
Chris@0
|
367 ]);
|
Chris@0
|
368 $field->save();
|
Chris@0
|
369
|
Chris@0
|
370 // Assign widget settings for the 'default' form mode.
|
Chris@0
|
371 entity_get_form_display('node', $type->id(), 'default')
|
Chris@0
|
372 ->setComponent('body', [
|
Chris@0
|
373 'type' => 'text_textarea_with_summary',
|
Chris@0
|
374 ])
|
Chris@0
|
375 ->save();
|
Chris@0
|
376
|
Chris@0
|
377 // Assign display settings for the 'default' and 'teaser' view modes.
|
Chris@0
|
378 entity_get_display('node', $type->id(), 'default')
|
Chris@0
|
379 ->setComponent('body', [
|
Chris@0
|
380 'label' => 'hidden',
|
Chris@0
|
381 'type' => 'text_default',
|
Chris@0
|
382 ])
|
Chris@0
|
383 ->save();
|
Chris@0
|
384
|
Chris@0
|
385 // The teaser view mode is created by the Standard profile and therefore
|
Chris@0
|
386 // might not exist.
|
Chris@18
|
387 $view_modes = \Drupal::service('entity_display.repository')->getViewModes('node');
|
Chris@0
|
388 if (isset($view_modes['teaser'])) {
|
Chris@0
|
389 entity_get_display('node', $type->id(), 'teaser')
|
Chris@0
|
390 ->setComponent('body', [
|
Chris@0
|
391 'label' => 'hidden',
|
Chris@0
|
392 'type' => 'text_summary_or_trimmed',
|
Chris@0
|
393 ])
|
Chris@0
|
394 ->save();
|
Chris@0
|
395 }
|
Chris@0
|
396 }
|
Chris@0
|
397
|
Chris@0
|
398 return $field;
|
Chris@0
|
399 }
|
Chris@0
|
400
|
Chris@0
|
401 /**
|
Chris@0
|
402 * Implements hook_entity_extra_field_info().
|
Chris@0
|
403 */
|
Chris@0
|
404 function node_entity_extra_field_info() {
|
Chris@0
|
405 $extra = [];
|
Chris@0
|
406 $description = t('Node module element');
|
Chris@0
|
407 foreach (NodeType::loadMultiple() as $bundle) {
|
Chris@0
|
408 $extra['node'][$bundle->id()]['display']['links'] = [
|
Chris@0
|
409 'label' => t('Links'),
|
Chris@0
|
410 'description' => $description,
|
Chris@0
|
411 'weight' => 100,
|
Chris@0
|
412 'visible' => TRUE,
|
Chris@0
|
413 ];
|
Chris@0
|
414 }
|
Chris@0
|
415
|
Chris@0
|
416 return $extra;
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 /**
|
Chris@0
|
420 * Updates all nodes of one type to be of another type.
|
Chris@0
|
421 *
|
Chris@0
|
422 * @param string $old_id
|
Chris@0
|
423 * The current node type of the nodes.
|
Chris@0
|
424 * @param string $new_id
|
Chris@0
|
425 * The new node type of the nodes.
|
Chris@0
|
426 *
|
Chris@0
|
427 * @return
|
Chris@0
|
428 * The number of nodes whose node type field was modified.
|
Chris@0
|
429 */
|
Chris@0
|
430 function node_type_update_nodes($old_id, $new_id) {
|
Chris@0
|
431 return \Drupal::entityManager()->getStorage('node')->updateType($old_id, $new_id);
|
Chris@0
|
432 }
|
Chris@0
|
433
|
Chris@0
|
434 /**
|
Chris@0
|
435 * Loads node entities from the database.
|
Chris@0
|
436 *
|
Chris@0
|
437 * This function should be used whenever you need to load more than one node
|
Chris@0
|
438 * from the database. Nodes are loaded into memory and will not require database
|
Chris@0
|
439 * access if loaded again during the same page request.
|
Chris@0
|
440 *
|
Chris@0
|
441 * @param array $nids
|
Chris@0
|
442 * (optional) An array of entity IDs. If omitted, all entities are loaded.
|
Chris@0
|
443 * @param bool $reset
|
Chris@0
|
444 * (optional) Whether to reset the internal node_load() cache. Defaults to
|
Chris@0
|
445 * FALSE.
|
Chris@0
|
446 *
|
Chris@0
|
447 * @return \Drupal\node\NodeInterface[]
|
Chris@0
|
448 * An array of node entities indexed by nid.
|
Chris@0
|
449 *
|
Chris@18
|
450 * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use
|
Chris@18
|
451 * \Drupal\node\Entity\Node::loadMultiple().
|
Chris@0
|
452 *
|
Chris@18
|
453 * @see https://www.drupal.org/node/2266845
|
Chris@0
|
454 */
|
Chris@0
|
455 function node_load_multiple(array $nids = NULL, $reset = FALSE) {
|
Chris@18
|
456 @trigger_error('node_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\node\Entity\Node::loadMultiple(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
|
Chris@0
|
457 if ($reset) {
|
Chris@0
|
458 \Drupal::entityManager()->getStorage('node')->resetCache($nids);
|
Chris@0
|
459 }
|
Chris@0
|
460 return Node::loadMultiple($nids);
|
Chris@0
|
461 }
|
Chris@0
|
462
|
Chris@0
|
463 /**
|
Chris@0
|
464 * Loads a node entity from the database.
|
Chris@0
|
465 *
|
Chris@0
|
466 * @param int $nid
|
Chris@0
|
467 * The node ID.
|
Chris@0
|
468 * @param bool $reset
|
Chris@0
|
469 * (optional) Whether to reset the node_load_multiple() cache. Defaults to
|
Chris@0
|
470 * FALSE.
|
Chris@0
|
471 *
|
Chris@0
|
472 * @return \Drupal\node\NodeInterface|null
|
Chris@0
|
473 * A fully-populated node entity, or NULL if the node is not found.
|
Chris@0
|
474 *
|
Chris@18
|
475 * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use
|
Chris@18
|
476 * \Drupal\node\Entity\Node::load().
|
Chris@18
|
477 *
|
Chris@18
|
478 * @see https://www.drupal.org/node/2266845
|
Chris@0
|
479 */
|
Chris@0
|
480 function node_load($nid = NULL, $reset = FALSE) {
|
Chris@18
|
481 @trigger_error('node_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\node\Entity\Node::load(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
|
Chris@0
|
482 if ($reset) {
|
Chris@0
|
483 \Drupal::entityManager()->getStorage('node')->resetCache([$nid]);
|
Chris@0
|
484 }
|
Chris@0
|
485 return Node::load($nid);
|
Chris@0
|
486 }
|
Chris@0
|
487
|
Chris@0
|
488 /**
|
Chris@0
|
489 * Loads a node revision from the database.
|
Chris@0
|
490 *
|
Chris@0
|
491 * @param int $vid
|
Chris@0
|
492 * The node revision id.
|
Chris@0
|
493 *
|
Chris@0
|
494 * @return \Drupal\node\NodeInterface|null
|
Chris@0
|
495 * A fully-populated node entity, or NULL if the node is not found.
|
Chris@0
|
496 */
|
Chris@0
|
497 function node_revision_load($vid = NULL) {
|
Chris@0
|
498 return \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 /**
|
Chris@0
|
502 * Deletes a node revision.
|
Chris@0
|
503 *
|
Chris@0
|
504 * @param int $revision_id
|
Chris@0
|
505 * The revision ID to delete.
|
Chris@0
|
506 */
|
Chris@0
|
507 function node_revision_delete($revision_id) {
|
Chris@0
|
508 \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($revision_id);
|
Chris@0
|
509 }
|
Chris@0
|
510
|
Chris@0
|
511 /**
|
Chris@0
|
512 * Checks whether the current page is the full page view of the passed-in node.
|
Chris@0
|
513 *
|
Chris@0
|
514 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
515 * A node entity.
|
Chris@0
|
516 *
|
Chris@0
|
517 * @return int|false
|
Chris@0
|
518 * The ID of the node if this is a full page view, otherwise FALSE.
|
Chris@0
|
519 */
|
Chris@0
|
520 function node_is_page(NodeInterface $node) {
|
Chris@0
|
521 $route_match = \Drupal::routeMatch();
|
Chris@0
|
522 if ($route_match->getRouteName() == 'entity.node.canonical') {
|
Chris@0
|
523 $page_node = $route_match->getParameter('node');
|
Chris@0
|
524 }
|
Chris@0
|
525 return (!empty($page_node) ? $page_node->id() == $node->id() : FALSE);
|
Chris@0
|
526 }
|
Chris@0
|
527
|
Chris@0
|
528 /**
|
Chris@0
|
529 * Prepares variables for list of available node type templates.
|
Chris@0
|
530 *
|
Chris@0
|
531 * Default template: node-add-list.html.twig.
|
Chris@0
|
532 *
|
Chris@0
|
533 * @param array $variables
|
Chris@0
|
534 * An associative array containing:
|
Chris@0
|
535 * - content: An array of content types.
|
Chris@0
|
536 *
|
Chris@18
|
537 * @see \Drupal\node\Controller\NodeController::addPage()
|
Chris@0
|
538 */
|
Chris@0
|
539 function template_preprocess_node_add_list(&$variables) {
|
Chris@0
|
540 $variables['types'] = [];
|
Chris@0
|
541 if (!empty($variables['content'])) {
|
Chris@0
|
542 foreach ($variables['content'] as $type) {
|
Chris@0
|
543 $variables['types'][$type->id()] = [
|
Chris@0
|
544 'type' => $type->id(),
|
Chris@0
|
545 'add_link' => \Drupal::l($type->label(), new Url('node.add', ['node_type' => $type->id()])),
|
Chris@0
|
546 'description' => [
|
Chris@0
|
547 '#markup' => $type->getDescription(),
|
Chris@0
|
548 ],
|
Chris@0
|
549 ];
|
Chris@0
|
550 }
|
Chris@0
|
551 }
|
Chris@0
|
552 }
|
Chris@0
|
553
|
Chris@0
|
554 /**
|
Chris@0
|
555 * Implements hook_preprocess_HOOK() for HTML document templates.
|
Chris@0
|
556 */
|
Chris@0
|
557 function node_preprocess_html(&$variables) {
|
Chris@17
|
558 // If on an individual node page or node preview page, add the node type to
|
Chris@17
|
559 // the body classes.
|
Chris@17
|
560 if (($node = \Drupal::routeMatch()->getParameter('node')) || ($node = \Drupal::routeMatch()->getParameter('node_preview'))) {
|
Chris@17
|
561 if ($node instanceof NodeInterface) {
|
Chris@17
|
562 $variables['node_type'] = $node->getType();
|
Chris@17
|
563 }
|
Chris@0
|
564 }
|
Chris@0
|
565 }
|
Chris@0
|
566
|
Chris@0
|
567 /**
|
Chris@0
|
568 * Implements hook_preprocess_HOOK() for block templates.
|
Chris@0
|
569 */
|
Chris@0
|
570 function node_preprocess_block(&$variables) {
|
Chris@0
|
571 if ($variables['configuration']['provider'] == 'node') {
|
Chris@0
|
572 switch ($variables['elements']['#plugin_id']) {
|
Chris@0
|
573 case 'node_syndicate_block':
|
Chris@0
|
574 $variables['attributes']['role'] = 'complementary';
|
Chris@0
|
575 break;
|
Chris@0
|
576 }
|
Chris@0
|
577 }
|
Chris@0
|
578 }
|
Chris@0
|
579
|
Chris@0
|
580 /**
|
Chris@0
|
581 * Implements hook_theme_suggestions_HOOK().
|
Chris@0
|
582 */
|
Chris@0
|
583 function node_theme_suggestions_node(array $variables) {
|
Chris@0
|
584 $suggestions = [];
|
Chris@0
|
585 $node = $variables['elements']['#node'];
|
Chris@0
|
586 $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
|
Chris@0
|
587
|
Chris@0
|
588 $suggestions[] = 'node__' . $sanitized_view_mode;
|
Chris@0
|
589 $suggestions[] = 'node__' . $node->bundle();
|
Chris@0
|
590 $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
|
Chris@0
|
591 $suggestions[] = 'node__' . $node->id();
|
Chris@0
|
592 $suggestions[] = 'node__' . $node->id() . '__' . $sanitized_view_mode;
|
Chris@0
|
593
|
Chris@0
|
594 return $suggestions;
|
Chris@0
|
595 }
|
Chris@0
|
596
|
Chris@0
|
597 /**
|
Chris@0
|
598 * Prepares variables for node templates.
|
Chris@0
|
599 *
|
Chris@0
|
600 * Default template: node.html.twig.
|
Chris@0
|
601 *
|
Chris@0
|
602 * Most themes use their own copy of node.html.twig. The default is located
|
Chris@0
|
603 * inside "/core/modules/node/templates/node.html.twig". Look in there for the
|
Chris@0
|
604 * full list of variables.
|
Chris@0
|
605 *
|
Chris@18
|
606 * By default this function performs special preprocessing of some base fields
|
Chris@18
|
607 * so they are available as variables in the template. For example 'title'
|
Chris@18
|
608 * appears as 'label'. This preprocessing is skipped if:
|
Chris@18
|
609 * - a module makes the field's display configurable via the field UI by means
|
Chris@18
|
610 * of BaseFieldDefinition::setDisplayConfigurable()
|
Chris@18
|
611 * - AND the additional entity type property
|
Chris@18
|
612 * 'enable_base_field_custom_preprocess_skipping' has been set using
|
Chris@18
|
613 * hook_entity_type_build().
|
Chris@18
|
614 *
|
Chris@0
|
615 * @param array $variables
|
Chris@0
|
616 * An associative array containing:
|
Chris@0
|
617 * - elements: An array of elements to display in view mode.
|
Chris@0
|
618 * - node: The node object.
|
Chris@0
|
619 * - view_mode: View mode; e.g., 'full', 'teaser', etc.
|
Chris@18
|
620 *
|
Chris@18
|
621 * @see hook_entity_type_build()
|
Chris@18
|
622 * @see \Drupal\Core\Field\BaseFieldDefinition::setDisplayConfigurable()
|
Chris@0
|
623 */
|
Chris@0
|
624 function template_preprocess_node(&$variables) {
|
Chris@0
|
625 $variables['view_mode'] = $variables['elements']['#view_mode'];
|
Chris@0
|
626 // Provide a distinct $teaser boolean.
|
Chris@0
|
627 $variables['teaser'] = $variables['view_mode'] == 'teaser';
|
Chris@0
|
628 $variables['node'] = $variables['elements']['#node'];
|
Chris@0
|
629 /** @var \Drupal\node\NodeInterface $node */
|
Chris@0
|
630 $node = $variables['node'];
|
Chris@18
|
631 $skip_custom_preprocessing = $node->getEntityType()->get('enable_base_field_custom_preprocess_skipping');
|
Chris@0
|
632
|
Chris@18
|
633 // Make created, uid and title fields available separately. Skip this custom
|
Chris@18
|
634 // preprocessing if the field display is configurable and skipping has been
|
Chris@18
|
635 // enabled.
|
Chris@18
|
636 // @todo https://www.drupal.org/project/drupal/issues/3015623
|
Chris@18
|
637 // In D9 delete this code and matching template lines. Using
|
Chris@18
|
638 // $variables['content'] is more flexible and consistent.
|
Chris@18
|
639 $submitted_configurable = $node->getFieldDefinition('created')->isDisplayConfigurable('view') || $node->getFieldDefinition('uid')->isDisplayConfigurable('view');
|
Chris@18
|
640 if (!$skip_custom_preprocessing || !$submitted_configurable) {
|
Chris@18
|
641 $variables['date'] = \Drupal::service('renderer')->render($variables['elements']['created']);
|
Chris@18
|
642 unset($variables['elements']['created']);
|
Chris@18
|
643 $variables['author_name'] = \Drupal::service('renderer')->render($variables['elements']['uid']);
|
Chris@18
|
644 unset($variables['elements']['uid']);
|
Chris@18
|
645 }
|
Chris@18
|
646
|
Chris@18
|
647 if (!$skip_custom_preprocessing || !$node->getFieldDefinition('title')->isDisplayConfigurable('view')) {
|
Chris@18
|
648 $variables['label'] = $variables['elements']['title'];
|
Chris@18
|
649 unset($variables['elements']['title']);
|
Chris@18
|
650 }
|
Chris@18
|
651
|
Chris@18
|
652 $variables['url'] = !$node->isNew() ? $node->toUrl('canonical')->toString() : NULL;
|
Chris@18
|
653
|
Chris@0
|
654 // The 'page' variable is set to TRUE in two occasions:
|
Chris@0
|
655 // - The view mode is 'full' and we are on the 'node.view' route.
|
Chris@0
|
656 // - The node is in preview and view mode is either 'full' or 'default'.
|
Chris@0
|
657 $variables['page'] = ($variables['view_mode'] == 'full' && (node_is_page($node)) || (isset($node->in_preview) && in_array($node->preview_view_mode, ['full', 'default'])));
|
Chris@0
|
658
|
Chris@0
|
659 // Helpful $content variable for templates.
|
Chris@0
|
660 $variables += ['content' => []];
|
Chris@0
|
661 foreach (Element::children($variables['elements']) as $key) {
|
Chris@0
|
662 $variables['content'][$key] = $variables['elements'][$key];
|
Chris@0
|
663 }
|
Chris@0
|
664
|
Chris@18
|
665 if (isset($variables['date'])) {
|
Chris@18
|
666 // Display post information on certain node types. This only occurs if
|
Chris@18
|
667 // custom preprocessing occurred for both of the created and uid fields.
|
Chris@18
|
668 // @todo https://www.drupal.org/project/drupal/issues/3015623
|
Chris@18
|
669 // In D9 delete this code and matching template lines. Using a field
|
Chris@18
|
670 // formatter is more flexible and consistent.
|
Chris@18
|
671 $node_type = $node->type->entity;
|
Chris@18
|
672 // Used by RDF to add attributes around the author and date submitted.
|
Chris@18
|
673 $variables['author_attributes'] = new Attribute();
|
Chris@18
|
674 $variables['display_submitted'] = $node_type->displaySubmitted();
|
Chris@18
|
675 if ($variables['display_submitted']) {
|
Chris@18
|
676 if (theme_get_setting('features.node_user_picture')) {
|
Chris@18
|
677 // To change user picture settings (e.g. image style), edit the
|
Chris@18
|
678 // 'compact' view mode on the User entity. Note that the 'compact'
|
Chris@18
|
679 // view mode might not be configured, so remember to always check the
|
Chris@18
|
680 // theme setting first.
|
Chris@18
|
681 $variables['author_picture'] = user_view($node->getOwner(), 'compact');
|
Chris@18
|
682 }
|
Chris@0
|
683 }
|
Chris@0
|
684 }
|
Chris@0
|
685
|
Chris@0
|
686 // Add article ARIA role.
|
Chris@0
|
687 $variables['attributes']['role'] = 'article';
|
Chris@0
|
688 }
|
Chris@0
|
689
|
Chris@0
|
690 /**
|
Chris@0
|
691 * Implements hook_cron().
|
Chris@0
|
692 */
|
Chris@0
|
693 function node_cron() {
|
Chris@0
|
694 // Calculate the oldest and newest node created times, for use in search
|
Chris@0
|
695 // rankings. (Note that field aliases have to be variables passed by
|
Chris@0
|
696 // reference.)
|
Chris@0
|
697 if (\Drupal::moduleHandler()->moduleExists('search')) {
|
Chris@0
|
698 $min_alias = 'min_created';
|
Chris@0
|
699 $max_alias = 'max_created';
|
Chris@0
|
700 $result = \Drupal::entityQueryAggregate('node')
|
Chris@0
|
701 ->aggregate('created', 'MIN', NULL, $min_alias)
|
Chris@0
|
702 ->aggregate('created', 'MAX', NULL, $max_alias)
|
Chris@0
|
703 ->execute();
|
Chris@0
|
704 if (isset($result[0])) {
|
Chris@0
|
705 // Make an array with definite keys and store it in the state system.
|
Chris@0
|
706 $array = [
|
Chris@0
|
707 'min_created' => $result[0][$min_alias],
|
Chris@0
|
708 'max_created' => $result[0][$max_alias],
|
Chris@0
|
709 ];
|
Chris@0
|
710 \Drupal::state()->set('node.min_max_update_time', $array);
|
Chris@0
|
711 }
|
Chris@0
|
712 }
|
Chris@0
|
713 }
|
Chris@0
|
714
|
Chris@0
|
715 /**
|
Chris@0
|
716 * Implements hook_ranking().
|
Chris@0
|
717 */
|
Chris@0
|
718 function node_ranking() {
|
Chris@0
|
719 // Create the ranking array and add the basic ranking options.
|
Chris@0
|
720 $ranking = [
|
Chris@0
|
721 'relevance' => [
|
Chris@0
|
722 'title' => t('Keyword relevance'),
|
Chris@0
|
723 // Average relevance values hover around 0.15
|
Chris@0
|
724 'score' => 'i.relevance',
|
Chris@0
|
725 ],
|
Chris@0
|
726 'sticky' => [
|
Chris@0
|
727 'title' => t('Content is sticky at top of lists'),
|
Chris@0
|
728 // The sticky flag is either 0 or 1, which is automatically normalized.
|
Chris@0
|
729 'score' => 'n.sticky',
|
Chris@0
|
730 ],
|
Chris@0
|
731 'promote' => [
|
Chris@0
|
732 'title' => t('Content is promoted to the front page'),
|
Chris@0
|
733 // The promote flag is either 0 or 1, which is automatically normalized.
|
Chris@0
|
734 'score' => 'n.promote',
|
Chris@0
|
735 ],
|
Chris@0
|
736 ];
|
Chris@0
|
737 // Add relevance based on updated date, but only if it the scale values have
|
Chris@0
|
738 // been calculated in node_cron().
|
Chris@0
|
739 if ($node_min_max = \Drupal::state()->get('node.min_max_update_time')) {
|
Chris@0
|
740 $ranking['recent'] = [
|
Chris@0
|
741 'title' => t('Recently created'),
|
Chris@0
|
742 // Exponential decay with half life of 14% of the age range of nodes.
|
Chris@0
|
743 'score' => 'EXP(-5 * (1 - (n.created - :node_oldest) / :node_range))',
|
Chris@0
|
744 'arguments' => [
|
Chris@0
|
745 ':node_oldest' => $node_min_max['min_created'],
|
Chris@0
|
746 ':node_range' => max($node_min_max['max_created'] - $node_min_max['min_created'], 1),
|
Chris@0
|
747 ],
|
Chris@0
|
748 ];
|
Chris@0
|
749 }
|
Chris@0
|
750 return $ranking;
|
Chris@0
|
751 }
|
Chris@0
|
752
|
Chris@0
|
753 /**
|
Chris@0
|
754 * Implements hook_user_cancel().
|
Chris@0
|
755 */
|
Chris@17
|
756 function node_user_cancel($edit, UserInterface $account, $method) {
|
Chris@0
|
757 switch ($method) {
|
Chris@0
|
758 case 'user_cancel_block_unpublish':
|
Chris@0
|
759 // Unpublish nodes (current revisions).
|
Chris@0
|
760 $nids = \Drupal::entityQuery('node')
|
Chris@0
|
761 ->condition('uid', $account->id())
|
Chris@0
|
762 ->execute();
|
Chris@0
|
763 module_load_include('inc', 'node', 'node.admin');
|
Chris@0
|
764 node_mass_update($nids, ['status' => 0], NULL, TRUE);
|
Chris@0
|
765 break;
|
Chris@0
|
766
|
Chris@0
|
767 case 'user_cancel_reassign':
|
Chris@0
|
768 // Anonymize all of the nodes for this old account.
|
Chris@0
|
769 module_load_include('inc', 'node', 'node.admin');
|
Chris@0
|
770 $vids = \Drupal::entityManager()->getStorage('node')->userRevisionIds($account);
|
Chris@0
|
771 node_mass_update($vids, [
|
Chris@0
|
772 'uid' => 0,
|
Chris@0
|
773 'revision_uid' => 0,
|
Chris@0
|
774 ], NULL, TRUE, TRUE);
|
Chris@0
|
775 break;
|
Chris@0
|
776 }
|
Chris@0
|
777 }
|
Chris@0
|
778
|
Chris@0
|
779 /**
|
Chris@0
|
780 * Implements hook_ENTITY_TYPE_predelete() for user entities.
|
Chris@0
|
781 */
|
Chris@0
|
782 function node_user_predelete($account) {
|
Chris@0
|
783 // Delete nodes (current revisions).
|
Chris@0
|
784 // @todo Introduce node_mass_delete() or make node_mass_update() more flexible.
|
Chris@0
|
785 $nids = \Drupal::entityQuery('node')
|
Chris@0
|
786 ->condition('uid', $account->id())
|
Chris@0
|
787 ->accessCheck(FALSE)
|
Chris@0
|
788 ->execute();
|
Chris@0
|
789 entity_delete_multiple('node', $nids);
|
Chris@0
|
790 // Delete old revisions.
|
Chris@0
|
791 $storage_controller = \Drupal::entityManager()->getStorage('node');
|
Chris@0
|
792 $revisions = $storage_controller->userRevisionIds($account);
|
Chris@0
|
793 foreach ($revisions as $revision) {
|
Chris@0
|
794 node_revision_delete($revision);
|
Chris@0
|
795 }
|
Chris@0
|
796 }
|
Chris@0
|
797
|
Chris@0
|
798 /**
|
Chris@0
|
799 * Finds the most recently changed nodes that are available to the current user.
|
Chris@0
|
800 *
|
Chris@0
|
801 * @param $number
|
Chris@0
|
802 * (optional) The maximum number of nodes to find. Defaults to 10.
|
Chris@0
|
803 *
|
Chris@0
|
804 * @return \Drupal\node\NodeInterface[]
|
Chris@0
|
805 * An array of node entities or an empty array if there are no recent nodes
|
Chris@0
|
806 * visible to the current user.
|
Chris@0
|
807 */
|
Chris@0
|
808 function node_get_recent($number = 10) {
|
Chris@0
|
809 $account = \Drupal::currentUser();
|
Chris@0
|
810 $query = \Drupal::entityQuery('node');
|
Chris@0
|
811
|
Chris@0
|
812 if (!$account->hasPermission('bypass node access')) {
|
Chris@0
|
813 // If the user is able to view their own unpublished nodes, allow them
|
Chris@0
|
814 // to see these in addition to published nodes. Check that they actually
|
Chris@0
|
815 // have some unpublished nodes to view before adding the condition.
|
Chris@0
|
816 $access_query = \Drupal::entityQuery('node')
|
Chris@0
|
817 ->condition('uid', $account->id())
|
Chris@0
|
818 ->condition('status', NodeInterface::NOT_PUBLISHED);
|
Chris@0
|
819 if ($account->hasPermission('view own unpublished content') && ($own_unpublished = $access_query->execute())) {
|
Chris@0
|
820 $query->orConditionGroup()
|
Chris@0
|
821 ->condition('status', NodeInterface::PUBLISHED)
|
Chris@0
|
822 ->condition('nid', $own_unpublished, 'IN');
|
Chris@0
|
823 }
|
Chris@0
|
824 else {
|
Chris@0
|
825 // If not, restrict the query to published nodes.
|
Chris@0
|
826 $query->condition('status', NodeInterface::PUBLISHED);
|
Chris@0
|
827 }
|
Chris@0
|
828 }
|
Chris@0
|
829 $nids = $query
|
Chris@0
|
830 ->sort('changed', 'DESC')
|
Chris@0
|
831 ->range(0, $number)
|
Chris@0
|
832 ->addTag('node_access')
|
Chris@0
|
833 ->execute();
|
Chris@0
|
834
|
Chris@0
|
835 $nodes = Node::loadMultiple($nids);
|
Chris@0
|
836
|
Chris@0
|
837 return $nodes ? $nodes : [];
|
Chris@0
|
838 }
|
Chris@0
|
839
|
Chris@0
|
840 /**
|
Chris@0
|
841 * Generates an array for rendering the given node.
|
Chris@0
|
842 *
|
Chris@0
|
843 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
844 * A node entity.
|
Chris@0
|
845 * @param $view_mode
|
Chris@0
|
846 * (optional) View mode, e.g., 'full', 'teaser', etc. Defaults to 'full.'
|
Chris@0
|
847 * @param $langcode
|
Chris@0
|
848 * (optional) A language code to use for rendering. Defaults to NULL which is
|
Chris@0
|
849 * the global content language of the current request.
|
Chris@0
|
850 *
|
Chris@0
|
851 * @return array
|
Chris@16
|
852 * An array as expected by \Drupal\Core\Render\RendererInterface::render().
|
Chris@0
|
853 */
|
Chris@0
|
854 function node_view(NodeInterface $node, $view_mode = 'full', $langcode = NULL) {
|
Chris@0
|
855 return entity_view($node, $view_mode, $langcode);
|
Chris@0
|
856 }
|
Chris@0
|
857
|
Chris@0
|
858 /**
|
Chris@0
|
859 * Constructs a drupal_render() style array from an array of loaded nodes.
|
Chris@0
|
860 *
|
Chris@0
|
861 * @param $nodes
|
Chris@0
|
862 * An array of nodes as returned by Node::loadMultiple().
|
Chris@0
|
863 * @param $view_mode
|
Chris@0
|
864 * (optional) View mode, e.g., 'full', 'teaser', etc. Defaults to 'teaser.'
|
Chris@0
|
865 * @param $langcode
|
Chris@0
|
866 * (optional) A language code to use for rendering. Defaults to the global
|
Chris@0
|
867 * content language of the current request.
|
Chris@0
|
868 *
|
Chris@0
|
869 * @return array
|
Chris@16
|
870 * An array in the format expected by
|
Chris@16
|
871 * \Drupal\Core\Render\RendererInterface::render().
|
Chris@0
|
872 */
|
Chris@0
|
873 function node_view_multiple($nodes, $view_mode = 'teaser', $langcode = NULL) {
|
Chris@0
|
874 return entity_view_multiple($nodes, $view_mode, $langcode);
|
Chris@0
|
875 }
|
Chris@0
|
876
|
Chris@0
|
877 /**
|
Chris@0
|
878 * Implements hook_page_top().
|
Chris@0
|
879 */
|
Chris@0
|
880 function node_page_top(array &$page) {
|
Chris@0
|
881 // Add 'Back to content editing' link on preview page.
|
Chris@0
|
882 $route_match = \Drupal::routeMatch();
|
Chris@0
|
883 if ($route_match->getRouteName() == 'entity.node.preview') {
|
Chris@0
|
884 $page['page_top']['node_preview'] = [
|
Chris@0
|
885 '#type' => 'container',
|
Chris@0
|
886 '#attributes' => [
|
Chris@17
|
887 'class' => ['node-preview-container', 'container-inline'],
|
Chris@0
|
888 ],
|
Chris@0
|
889 ];
|
Chris@0
|
890
|
Chris@0
|
891 $form = \Drupal::formBuilder()->getForm('\Drupal\node\Form\NodePreviewForm', $route_match->getParameter('node_preview'));
|
Chris@0
|
892 $page['page_top']['node_preview']['view_mode'] = $form;
|
Chris@0
|
893 }
|
Chris@0
|
894 }
|
Chris@0
|
895
|
Chris@0
|
896 /**
|
Chris@0
|
897 * Implements hook_form_FORM_ID_alter().
|
Chris@0
|
898 *
|
Chris@0
|
899 * Alters the theme form to use the admin theme on node editing.
|
Chris@0
|
900 *
|
Chris@0
|
901 * @see node_form_system_themes_admin_form_submit()
|
Chris@0
|
902 */
|
Chris@0
|
903 function node_form_system_themes_admin_form_alter(&$form, FormStateInterface $form_state, $form_id) {
|
Chris@0
|
904 $form['admin_theme']['use_admin_theme'] = [
|
Chris@0
|
905 '#type' => 'checkbox',
|
Chris@0
|
906 '#title' => t('Use the administration theme when editing or creating content'),
|
Chris@0
|
907 '#description' => t('Control which roles can "View the administration theme" on the <a href=":permissions">Permissions page</a>.', [':permissions' => Url::fromRoute('user.admin_permissions')->toString()]),
|
Chris@0
|
908 '#default_value' => \Drupal::configFactory()->getEditable('node.settings')->get('use_admin_theme'),
|
Chris@0
|
909 ];
|
Chris@0
|
910 $form['#submit'][] = 'node_form_system_themes_admin_form_submit';
|
Chris@0
|
911 }
|
Chris@0
|
912
|
Chris@0
|
913 /**
|
Chris@0
|
914 * Form submission handler for system_themes_admin_form().
|
Chris@0
|
915 *
|
Chris@0
|
916 * @see node_form_system_themes_admin_form_alter()
|
Chris@0
|
917 */
|
Chris@0
|
918 function node_form_system_themes_admin_form_submit($form, FormStateInterface $form_state) {
|
Chris@0
|
919 \Drupal::configFactory()->getEditable('node.settings')
|
Chris@0
|
920 ->set('use_admin_theme', $form_state->getValue('use_admin_theme'))
|
Chris@0
|
921 ->save();
|
Chris@0
|
922 }
|
Chris@0
|
923
|
Chris@0
|
924 /**
|
Chris@0
|
925 * @defgroup node_access Node access rights
|
Chris@0
|
926 * @{
|
Chris@0
|
927 * The node access system determines who can do what to which nodes.
|
Chris@0
|
928 *
|
Chris@0
|
929 * In determining access rights for a node, \Drupal\node\NodeAccessControlHandler
|
Chris@0
|
930 * first checks whether the user has the "bypass node access" permission. Such
|
Chris@0
|
931 * users have unrestricted access to all nodes. user 1 will always pass this
|
Chris@0
|
932 * check.
|
Chris@0
|
933 *
|
Chris@0
|
934 * Next, all implementations of hook_node_access() will be called. Each
|
Chris@0
|
935 * implementation may explicitly allow, explicitly forbid, or ignore the access
|
Chris@0
|
936 * request. If at least one module says to forbid the request, it will be
|
Chris@0
|
937 * rejected. If no modules deny the request and at least one says to allow it,
|
Chris@0
|
938 * the request will be permitted.
|
Chris@0
|
939 *
|
Chris@0
|
940 * If all modules ignore the access request, then the node_access table is used
|
Chris@0
|
941 * to determine access. All node access modules are queried using
|
Chris@0
|
942 * hook_node_grants() to assemble a list of "grant IDs" for the user. This list
|
Chris@0
|
943 * is compared against the table. If any row contains the node ID in question
|
Chris@0
|
944 * (or 0, which stands for "all nodes"), one of the grant IDs returned, and a
|
Chris@0
|
945 * value of TRUE for the operation in question, then access is granted. Note
|
Chris@0
|
946 * that this table is a list of grants; any matching row is sufficient to grant
|
Chris@0
|
947 * access to the node.
|
Chris@0
|
948 *
|
Chris@0
|
949 * In node listings (lists of nodes generated from a select query, such as the
|
Chris@0
|
950 * default home page at path 'node', an RSS feed, a recent content block, etc.),
|
Chris@0
|
951 * the process above is followed except that hook_node_access() is not called on
|
Chris@0
|
952 * each node for performance reasons and for proper functioning of the pager
|
Chris@0
|
953 * system. When adding a node listing to your module, be sure to use an entity
|
Chris@0
|
954 * query, which will add a tag of "node_access". This will allow modules dealing
|
Chris@0
|
955 * with node access to ensure only nodes to which the user has access are
|
Chris@0
|
956 * retrieved, through the use of hook_query_TAG_alter(). See the
|
Chris@0
|
957 * @link entity_api Entity API topic @endlink for more information on entity
|
Chris@0
|
958 * queries. Tagging a query with "node_access" does not check the
|
Chris@0
|
959 * published/unpublished status of nodes, so the base query is responsible
|
Chris@0
|
960 * for ensuring that unpublished nodes are not displayed to inappropriate users.
|
Chris@0
|
961 *
|
Chris@0
|
962 * Note: Even a single module returning an AccessResultInterface object from
|
Chris@0
|
963 * hook_node_access() whose isForbidden() method equals TRUE will block access
|
Chris@0
|
964 * to the node. Therefore, implementers should take care to not deny access
|
Chris@0
|
965 * unless they really intend to. Unless a module wishes to actively forbid
|
Chris@0
|
966 * access it should return an AccessResultInterface object whose isAllowed() nor
|
Chris@0
|
967 * isForbidden() methods return TRUE, to allow other modules or the node_access
|
Chris@0
|
968 * table to control access.
|
Chris@0
|
969 *
|
Chris@0
|
970 * To see how to write a node access module of your own, see
|
Chris@0
|
971 * node_access_example.module.
|
Chris@0
|
972 */
|
Chris@0
|
973
|
Chris@0
|
974 /**
|
Chris@0
|
975 * Implements hook_node_access().
|
Chris@0
|
976 */
|
Chris@18
|
977 function node_node_access(NodeInterface $node, $op, AccountInterface $account) {
|
Chris@0
|
978 $type = $node->bundle();
|
Chris@18
|
979 $access = AccessResult::neutral();
|
Chris@0
|
980
|
Chris@0
|
981 switch ($op) {
|
Chris@0
|
982 case 'create':
|
Chris@18
|
983 $access = AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' content');
|
Chris@0
|
984
|
Chris@0
|
985 case 'update':
|
Chris@18
|
986 $access = AccessResult::allowedIfHasPermission($account, 'edit any ' . $type . ' content');
|
Chris@18
|
987 if (!$access->isAllowed() && $account->hasPermission('edit own ' . $type . ' content')) {
|
Chris@18
|
988 $access = $access->orIf(AccessResult::allowedIf($account->id() == $node->getOwnerId())->cachePerUser()->addCacheableDependency($node));
|
Chris@0
|
989 }
|
Chris@18
|
990 break;
|
Chris@0
|
991
|
Chris@0
|
992 case 'delete':
|
Chris@18
|
993 $access = AccessResult::allowedIfHasPermission($account, 'delete any ' . $type . ' content');
|
Chris@18
|
994 if (!$access->isAllowed() && $account->hasPermission('delete own ' . $type . ' content')) {
|
Chris@18
|
995 $access = $access->orIf(AccessResult::allowedIf($account->id() == $node->getOwnerId()))->cachePerUser()->addCacheableDependency($node);
|
Chris@0
|
996 }
|
Chris@18
|
997 break;
|
Chris@18
|
998 }
|
Chris@0
|
999
|
Chris@18
|
1000 return $access;
|
Chris@0
|
1001 }
|
Chris@0
|
1002
|
Chris@0
|
1003 /**
|
Chris@0
|
1004 * Fetches an array of permission IDs granted to the given user ID.
|
Chris@0
|
1005 *
|
Chris@0
|
1006 * The implementation here provides only the universal "all" grant. A node
|
Chris@0
|
1007 * access module should implement hook_node_grants() to provide a grant list for
|
Chris@0
|
1008 * the user.
|
Chris@0
|
1009 *
|
Chris@0
|
1010 * After the default grants have been loaded, we allow modules to alter the
|
Chris@0
|
1011 * grants array by reference. This hook allows for complex business logic to be
|
Chris@0
|
1012 * applied when integrating multiple node access modules.
|
Chris@0
|
1013 *
|
Chris@0
|
1014 * @param string $op
|
Chris@0
|
1015 * The operation that the user is trying to perform.
|
Chris@0
|
1016 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
1017 * The account object for the user performing the operation.
|
Chris@0
|
1018 *
|
Chris@0
|
1019 * @return array
|
Chris@0
|
1020 * An associative array in which the keys are realms, and the values are
|
Chris@0
|
1021 * arrays of grants for those realms.
|
Chris@0
|
1022 */
|
Chris@0
|
1023 function node_access_grants($op, AccountInterface $account) {
|
Chris@0
|
1024 // Fetch node access grants from other modules.
|
Chris@0
|
1025 $grants = \Drupal::moduleHandler()->invokeAll('node_grants', [$account, $op]);
|
Chris@0
|
1026 // Allow modules to alter the assigned grants.
|
Chris@0
|
1027 \Drupal::moduleHandler()->alter('node_grants', $grants, $account, $op);
|
Chris@0
|
1028
|
Chris@0
|
1029 return array_merge(['all' => [0]], $grants);
|
Chris@0
|
1030 }
|
Chris@0
|
1031
|
Chris@0
|
1032 /**
|
Chris@0
|
1033 * Determines whether the user has a global viewing grant for all nodes.
|
Chris@0
|
1034 *
|
Chris@0
|
1035 * Checks to see whether any module grants global 'view' access to a user
|
Chris@0
|
1036 * account; global 'view' access is encoded in the {node_access} table as a
|
Chris@0
|
1037 * grant with nid=0. If no node access modules are enabled, node.module defines
|
Chris@0
|
1038 * such a global 'view' access grant.
|
Chris@0
|
1039 *
|
Chris@0
|
1040 * This function is called when a node listing query is tagged with
|
Chris@0
|
1041 * 'node_access'; when this function returns TRUE, no node access joins are
|
Chris@0
|
1042 * added to the query.
|
Chris@0
|
1043 *
|
Chris@0
|
1044 * @param $account
|
Chris@0
|
1045 * (optional) The user object for the user whose access is being checked. If
|
Chris@0
|
1046 * omitted, the current user is used. Defaults to NULL.
|
Chris@0
|
1047 *
|
Chris@0
|
1048 * @return
|
Chris@0
|
1049 * TRUE if 'view' access to all nodes is granted, FALSE otherwise.
|
Chris@0
|
1050 *
|
Chris@0
|
1051 * @see hook_node_grants()
|
Chris@0
|
1052 * @see node_query_node_access_alter()
|
Chris@0
|
1053 */
|
Chris@0
|
1054 function node_access_view_all_nodes($account = NULL) {
|
Chris@0
|
1055
|
Chris@0
|
1056 if (!$account) {
|
Chris@0
|
1057 $account = \Drupal::currentUser();
|
Chris@0
|
1058 }
|
Chris@0
|
1059
|
Chris@0
|
1060 // Statically cache results in an array keyed by $account->id().
|
Chris@0
|
1061 $access = &drupal_static(__FUNCTION__);
|
Chris@0
|
1062 if (isset($access[$account->id()])) {
|
Chris@0
|
1063 return $access[$account->id()];
|
Chris@0
|
1064 }
|
Chris@0
|
1065
|
Chris@0
|
1066 // If no modules implement the node access system, access is always TRUE.
|
Chris@0
|
1067 if (!\Drupal::moduleHandler()->getImplementations('node_grants')) {
|
Chris@0
|
1068 $access[$account->id()] = TRUE;
|
Chris@0
|
1069 }
|
Chris@0
|
1070 else {
|
Chris@0
|
1071 $access[$account->id()] = \Drupal::entityManager()->getAccessControlHandler('node')->checkAllGrants($account);
|
Chris@0
|
1072 }
|
Chris@0
|
1073
|
Chris@0
|
1074 return $access[$account->id()];
|
Chris@0
|
1075 }
|
Chris@0
|
1076
|
Chris@0
|
1077 /**
|
Chris@0
|
1078 * Implements hook_query_TAG_alter().
|
Chris@0
|
1079 *
|
Chris@0
|
1080 * This is the hook_query_alter() for queries tagged with 'node_access'. It adds
|
Chris@0
|
1081 * node access checks for the user account given by the 'account' meta-data (or
|
Chris@0
|
1082 * current user if not provided), for an operation given by the 'op' meta-data
|
Chris@0
|
1083 * (or 'view' if not provided; other possible values are 'update' and 'delete').
|
Chris@0
|
1084 *
|
Chris@0
|
1085 * Queries tagged with 'node_access' that are not against the {node} table
|
Chris@0
|
1086 * must add the base table as metadata. For example:
|
Chris@0
|
1087 * @code
|
Chris@0
|
1088 * $query
|
Chris@0
|
1089 * ->addTag('node_access')
|
Chris@0
|
1090 * ->addMetaData('base_table', 'taxonomy_index');
|
Chris@0
|
1091 * @endcode
|
Chris@0
|
1092 */
|
Chris@0
|
1093 function node_query_node_access_alter(AlterableInterface $query) {
|
Chris@0
|
1094 // Read meta-data from query, if provided.
|
Chris@0
|
1095 if (!$account = $query->getMetaData('account')) {
|
Chris@0
|
1096 $account = \Drupal::currentUser();
|
Chris@0
|
1097 }
|
Chris@0
|
1098 if (!$op = $query->getMetaData('op')) {
|
Chris@0
|
1099 $op = 'view';
|
Chris@0
|
1100 }
|
Chris@0
|
1101
|
Chris@0
|
1102 // If $account can bypass node access, or there are no node access modules,
|
Chris@0
|
1103 // or the operation is 'view' and the $account has a global view grant
|
Chris@0
|
1104 // (such as a view grant for node ID 0), we don't need to alter the query.
|
Chris@0
|
1105 if ($account->hasPermission('bypass node access')) {
|
Chris@0
|
1106 return;
|
Chris@0
|
1107 }
|
Chris@0
|
1108 if (!count(\Drupal::moduleHandler()->getImplementations('node_grants'))) {
|
Chris@0
|
1109 return;
|
Chris@0
|
1110 }
|
Chris@0
|
1111 if ($op == 'view' && node_access_view_all_nodes($account)) {
|
Chris@0
|
1112 return;
|
Chris@0
|
1113 }
|
Chris@0
|
1114
|
Chris@0
|
1115 $tables = $query->getTables();
|
Chris@0
|
1116 $base_table = $query->getMetaData('base_table');
|
Chris@0
|
1117 // If the base table is not given, default to one of the node base tables.
|
Chris@0
|
1118 if (!$base_table) {
|
Chris@0
|
1119 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
Chris@0
|
1120 $table_mapping = \Drupal::entityTypeManager()->getStorage('node')->getTableMapping();
|
Chris@0
|
1121 $node_base_tables = $table_mapping->getTableNames();
|
Chris@0
|
1122
|
Chris@0
|
1123 foreach ($tables as $table_info) {
|
Chris@0
|
1124 if (!($table_info instanceof SelectInterface)) {
|
Chris@0
|
1125 $table = $table_info['table'];
|
Chris@0
|
1126 // Ensure that 'node' and 'node_field_data' are always preferred over
|
Chris@0
|
1127 // 'node_revision' and 'node_field_revision'.
|
Chris@0
|
1128 if ($table == 'node' || $table == 'node_field_data') {
|
Chris@0
|
1129 $base_table = $table;
|
Chris@0
|
1130 break;
|
Chris@0
|
1131 }
|
Chris@0
|
1132 // If one of the node base tables are in the query, add it to the list
|
Chris@0
|
1133 // of possible base tables to join against.
|
Chris@0
|
1134 if (in_array($table, $node_base_tables)) {
|
Chris@0
|
1135 $base_table = $table;
|
Chris@0
|
1136 }
|
Chris@0
|
1137 }
|
Chris@0
|
1138 }
|
Chris@0
|
1139
|
Chris@0
|
1140 // Bail out if the base table is missing.
|
Chris@0
|
1141 if (!$base_table) {
|
Chris@0
|
1142 throw new Exception(t('Query tagged for node access but there is no node table, specify the base_table using meta data.'));
|
Chris@0
|
1143 }
|
Chris@0
|
1144 }
|
Chris@0
|
1145
|
Chris@0
|
1146 // Update the query for the given storage method.
|
Chris@0
|
1147 \Drupal::service('node.grant_storage')->alterQuery($query, $tables, $op, $account, $base_table);
|
Chris@0
|
1148
|
Chris@0
|
1149 // Bubble the 'user.node_grants:$op' cache context to the current render
|
Chris@0
|
1150 // context.
|
Chris@0
|
1151 $request = \Drupal::requestStack()->getCurrentRequest();
|
Chris@0
|
1152 $renderer = \Drupal::service('renderer');
|
Chris@0
|
1153 if ($request->isMethodCacheable() && $renderer->hasRenderContext()) {
|
Chris@0
|
1154 $build = ['#cache' => ['contexts' => ['user.node_grants:' . $op]]];
|
Chris@0
|
1155 $renderer->render($build);
|
Chris@0
|
1156 }
|
Chris@0
|
1157 }
|
Chris@0
|
1158
|
Chris@0
|
1159 /**
|
Chris@0
|
1160 * Toggles or reads the value of a flag for rebuilding the node access grants.
|
Chris@0
|
1161 *
|
Chris@0
|
1162 * When the flag is set, a message is displayed to users with 'access
|
Chris@0
|
1163 * administration pages' permission, pointing to the 'rebuild' confirm form.
|
Chris@0
|
1164 * This can be used as an alternative to direct node_access_rebuild calls,
|
Chris@0
|
1165 * allowing administrators to decide when they want to perform the actual
|
Chris@0
|
1166 * (possibly time consuming) rebuild.
|
Chris@0
|
1167 *
|
Chris@0
|
1168 * When unsure if the current user is an administrator, node_access_rebuild()
|
Chris@0
|
1169 * should be used instead.
|
Chris@0
|
1170 *
|
Chris@0
|
1171 * @param $rebuild
|
Chris@0
|
1172 * (optional) The boolean value to be written.
|
Chris@0
|
1173 *
|
Chris@0
|
1174 * @return bool|null
|
Chris@0
|
1175 * The current value of the flag if no value was provided for $rebuild. If a
|
Chris@0
|
1176 * value was provided for $rebuild, nothing (NULL) is returned.
|
Chris@0
|
1177 *
|
Chris@0
|
1178 * @see node_access_rebuild()
|
Chris@0
|
1179 */
|
Chris@0
|
1180 function node_access_needs_rebuild($rebuild = NULL) {
|
Chris@0
|
1181 if (!isset($rebuild)) {
|
Chris@0
|
1182 return \Drupal::state()->get('node.node_access_needs_rebuild') ?: FALSE;
|
Chris@0
|
1183 }
|
Chris@0
|
1184 elseif ($rebuild) {
|
Chris@0
|
1185 \Drupal::state()->set('node.node_access_needs_rebuild', TRUE);
|
Chris@0
|
1186 }
|
Chris@0
|
1187 else {
|
Chris@0
|
1188 \Drupal::state()->delete('node.node_access_needs_rebuild');
|
Chris@0
|
1189 }
|
Chris@0
|
1190 }
|
Chris@0
|
1191
|
Chris@0
|
1192 /**
|
Chris@0
|
1193 * Rebuilds the node access database.
|
Chris@0
|
1194 *
|
Chris@0
|
1195 * This rebuild is occasionally needed by modules that make system-wide changes
|
Chris@0
|
1196 * to access levels. When the rebuild is required by an admin-triggered action
|
Chris@0
|
1197 * (e.g module settings form), calling node_access_needs_rebuild(TRUE) instead
|
Chris@0
|
1198 * of node_access_rebuild() lets the user perform his changes and actually
|
Chris@0
|
1199 * rebuild only once he is done.
|
Chris@0
|
1200 *
|
Chris@0
|
1201 * Note : As of Drupal 6, node access modules are not required to (and actually
|
Chris@0
|
1202 * should not) call node_access_rebuild() in hook_install/uninstall anymore.
|
Chris@0
|
1203 *
|
Chris@0
|
1204 * @param $batch_mode
|
Chris@0
|
1205 * (optional) Set to TRUE to process in 'batch' mode, spawning processing over
|
Chris@0
|
1206 * several HTTP requests (thus avoiding the risk of PHP timeout if the site
|
Chris@0
|
1207 * has a large number of nodes). hook_update_N() and any form submit handler
|
Chris@0
|
1208 * are safe contexts to use the 'batch mode'. Less decidable cases (such as
|
Chris@0
|
1209 * calls from hook_user(), hook_taxonomy(), etc.) might consider using the
|
Chris@0
|
1210 * non-batch mode. Defaults to FALSE.
|
Chris@0
|
1211 *
|
Chris@0
|
1212 * @see node_access_needs_rebuild()
|
Chris@0
|
1213 */
|
Chris@0
|
1214 function node_access_rebuild($batch_mode = FALSE) {
|
Chris@0
|
1215 $node_storage = \Drupal::entityManager()->getStorage('node');
|
Chris@0
|
1216 /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
|
Chris@0
|
1217 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
|
Chris@0
|
1218 $access_control_handler->deleteGrants();
|
Chris@0
|
1219 // Only recalculate if the site is using a node_access module.
|
Chris@0
|
1220 if (count(\Drupal::moduleHandler()->getImplementations('node_grants'))) {
|
Chris@0
|
1221 if ($batch_mode) {
|
Chris@0
|
1222 $batch = [
|
Chris@0
|
1223 'title' => t('Rebuilding content access permissions'),
|
Chris@0
|
1224 'operations' => [
|
Chris@0
|
1225 ['_node_access_rebuild_batch_operation', []],
|
Chris@0
|
1226 ],
|
Chris@17
|
1227 'finished' => '_node_access_rebuild_batch_finished',
|
Chris@0
|
1228 ];
|
Chris@0
|
1229 batch_set($batch);
|
Chris@0
|
1230 }
|
Chris@0
|
1231 else {
|
Chris@0
|
1232 // Try to allocate enough time to rebuild node grants
|
Chris@18
|
1233 Environment::setTimeLimit(240);
|
Chris@0
|
1234
|
Chris@0
|
1235 // Rebuild newest nodes first so that recent content becomes available
|
Chris@0
|
1236 // quickly.
|
Chris@0
|
1237 $entity_query = \Drupal::entityQuery('node');
|
Chris@0
|
1238 $entity_query->sort('nid', 'DESC');
|
Chris@0
|
1239 // Disable access checking since all nodes must be processed even if the
|
Chris@0
|
1240 // user does not have access. And unless the current user has the bypass
|
Chris@0
|
1241 // node access permission, no nodes are accessible since the grants have
|
Chris@0
|
1242 // just been deleted.
|
Chris@0
|
1243 $entity_query->accessCheck(FALSE);
|
Chris@0
|
1244 $nids = $entity_query->execute();
|
Chris@0
|
1245 foreach ($nids as $nid) {
|
Chris@0
|
1246 $node_storage->resetCache([$nid]);
|
Chris@0
|
1247 $node = Node::load($nid);
|
Chris@0
|
1248 // To preserve database integrity, only write grants if the node
|
Chris@0
|
1249 // loads successfully.
|
Chris@0
|
1250 if (!empty($node)) {
|
Chris@0
|
1251 $grants = $access_control_handler->acquireGrants($node);
|
Chris@0
|
1252 \Drupal::service('node.grant_storage')->write($node, $grants);
|
Chris@0
|
1253 }
|
Chris@0
|
1254 }
|
Chris@0
|
1255 }
|
Chris@0
|
1256 }
|
Chris@0
|
1257 else {
|
Chris@0
|
1258 // Not using any node_access modules. Add the default grant.
|
Chris@0
|
1259 $access_control_handler->writeDefaultGrant();
|
Chris@0
|
1260 }
|
Chris@0
|
1261
|
Chris@0
|
1262 if (!isset($batch)) {
|
Chris@17
|
1263 \Drupal::messenger()->addStatus(t('Content permissions have been rebuilt.'));
|
Chris@0
|
1264 node_access_needs_rebuild(FALSE);
|
Chris@0
|
1265 }
|
Chris@0
|
1266 }
|
Chris@0
|
1267
|
Chris@0
|
1268 /**
|
Chris@0
|
1269 * Implements callback_batch_operation().
|
Chris@0
|
1270 *
|
Chris@0
|
1271 * Performs batch operation for node_access_rebuild().
|
Chris@0
|
1272 *
|
Chris@0
|
1273 * This is a multistep operation: we go through all nodes by packs of 20. The
|
Chris@0
|
1274 * batch processing engine interrupts processing and sends progress feedback
|
Chris@0
|
1275 * after 1 second execution time.
|
Chris@0
|
1276 *
|
Chris@0
|
1277 * @param array $context
|
Chris@0
|
1278 * An array of contextual key/value information for rebuild batch process.
|
Chris@0
|
1279 */
|
Chris@0
|
1280 function _node_access_rebuild_batch_operation(&$context) {
|
Chris@0
|
1281 $node_storage = \Drupal::entityManager()->getStorage('node');
|
Chris@0
|
1282 if (empty($context['sandbox'])) {
|
Chris@0
|
1283 // Initiate multistep processing.
|
Chris@0
|
1284 $context['sandbox']['progress'] = 0;
|
Chris@0
|
1285 $context['sandbox']['current_node'] = 0;
|
Chris@0
|
1286 $context['sandbox']['max'] = \Drupal::entityQuery('node')->accessCheck(FALSE)->count()->execute();
|
Chris@0
|
1287 }
|
Chris@0
|
1288
|
Chris@0
|
1289 // Process the next 20 nodes.
|
Chris@0
|
1290 $limit = 20;
|
Chris@0
|
1291 $nids = \Drupal::entityQuery('node')
|
Chris@0
|
1292 ->condition('nid', $context['sandbox']['current_node'], '>')
|
Chris@0
|
1293 ->sort('nid', 'ASC')
|
Chris@0
|
1294 // Disable access checking since all nodes must be processed even if the
|
Chris@0
|
1295 // user does not have access. And unless the current user has the bypass
|
Chris@0
|
1296 // node access permission, no nodes are accessible since the grants have
|
Chris@0
|
1297 // just been deleted.
|
Chris@0
|
1298 ->accessCheck(FALSE)
|
Chris@0
|
1299 ->range(0, $limit)
|
Chris@0
|
1300 ->execute();
|
Chris@0
|
1301 $node_storage->resetCache($nids);
|
Chris@0
|
1302 $nodes = Node::loadMultiple($nids);
|
Chris@0
|
1303 foreach ($nodes as $nid => $node) {
|
Chris@0
|
1304 // To preserve database integrity, only write grants if the node
|
Chris@0
|
1305 // loads successfully.
|
Chris@0
|
1306 if (!empty($node)) {
|
Chris@0
|
1307 /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
|
Chris@0
|
1308 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
|
Chris@0
|
1309 $grants = $access_control_handler->acquireGrants($node);
|
Chris@0
|
1310 \Drupal::service('node.grant_storage')->write($node, $grants);
|
Chris@0
|
1311 }
|
Chris@0
|
1312 $context['sandbox']['progress']++;
|
Chris@0
|
1313 $context['sandbox']['current_node'] = $nid;
|
Chris@0
|
1314 }
|
Chris@0
|
1315
|
Chris@0
|
1316 // Multistep processing : report progress.
|
Chris@0
|
1317 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
|
Chris@0
|
1318 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
|
Chris@0
|
1319 }
|
Chris@0
|
1320 }
|
Chris@0
|
1321
|
Chris@0
|
1322 /**
|
Chris@0
|
1323 * Implements callback_batch_finished().
|
Chris@0
|
1324 *
|
Chris@0
|
1325 * Performs post-processing for node_access_rebuild().
|
Chris@0
|
1326 *
|
Chris@0
|
1327 * @param bool $success
|
Chris@0
|
1328 * A boolean indicating whether the re-build process has completed.
|
Chris@0
|
1329 * @param array $results
|
Chris@0
|
1330 * An array of results information.
|
Chris@0
|
1331 * @param array $operations
|
Chris@0
|
1332 * An array of function calls (not used in this function).
|
Chris@0
|
1333 */
|
Chris@0
|
1334 function _node_access_rebuild_batch_finished($success, $results, $operations) {
|
Chris@0
|
1335 if ($success) {
|
Chris@17
|
1336 \Drupal::messenger()->addStatus(t('The content access permissions have been rebuilt.'));
|
Chris@0
|
1337 node_access_needs_rebuild(FALSE);
|
Chris@0
|
1338 }
|
Chris@0
|
1339 else {
|
Chris@17
|
1340 \Drupal::messenger()->addError(t('The content access permissions have not been properly rebuilt.'));
|
Chris@0
|
1341 }
|
Chris@0
|
1342 }
|
Chris@0
|
1343
|
Chris@0
|
1344 /**
|
Chris@0
|
1345 * @} End of "defgroup node_access".
|
Chris@0
|
1346 */
|
Chris@0
|
1347
|
Chris@0
|
1348 /**
|
Chris@0
|
1349 * Implements hook_modules_installed().
|
Chris@0
|
1350 */
|
Chris@0
|
1351 function node_modules_installed($modules) {
|
Chris@0
|
1352 // Check if any of the newly enabled modules require the node_access table to
|
Chris@0
|
1353 // be rebuilt.
|
Chris@0
|
1354 if (!node_access_needs_rebuild() && array_intersect($modules, \Drupal::moduleHandler()->getImplementations('node_grants'))) {
|
Chris@0
|
1355 node_access_needs_rebuild(TRUE);
|
Chris@0
|
1356 }
|
Chris@0
|
1357 }
|
Chris@0
|
1358
|
Chris@0
|
1359 /**
|
Chris@0
|
1360 * Implements hook_modules_uninstalled().
|
Chris@0
|
1361 */
|
Chris@0
|
1362 function node_modules_uninstalled($modules) {
|
Chris@0
|
1363 // Check whether any of the disabled modules implemented hook_node_grants(),
|
Chris@0
|
1364 // in which case the node access table needs to be rebuilt.
|
Chris@0
|
1365 foreach ($modules as $module) {
|
Chris@0
|
1366 // At this point, the module is already disabled, but its code is still
|
Chris@0
|
1367 // loaded in memory. Module functions must no longer be called. We only
|
Chris@0
|
1368 // check whether a hook implementation function exists and do not invoke it.
|
Chris@0
|
1369 // Node access also needs to be rebuilt if language module is disabled to
|
Chris@0
|
1370 // remove any language-specific grants.
|
Chris@0
|
1371 if (!node_access_needs_rebuild() && (\Drupal::moduleHandler()->implementsHook($module, 'node_grants') || $module == 'language')) {
|
Chris@0
|
1372 node_access_needs_rebuild(TRUE);
|
Chris@0
|
1373 }
|
Chris@0
|
1374 }
|
Chris@0
|
1375
|
Chris@0
|
1376 // If there remains no more node_access module, rebuilding will be
|
Chris@0
|
1377 // straightforward, we can do it right now.
|
Chris@0
|
1378 if (node_access_needs_rebuild() && count(\Drupal::moduleHandler()->getImplementations('node_grants')) == 0) {
|
Chris@0
|
1379 node_access_rebuild();
|
Chris@0
|
1380 }
|
Chris@0
|
1381 }
|
Chris@0
|
1382
|
Chris@0
|
1383 /**
|
Chris@0
|
1384 * Implements hook_ENTITY_TYPE_delete() for 'configurable_language'.
|
Chris@0
|
1385 */
|
Chris@0
|
1386 function node_configurable_language_delete(ConfigurableLanguageInterface $language) {
|
Chris@0
|
1387 // On nodes with this language, unset the language.
|
Chris@0
|
1388 \Drupal::entityManager()->getStorage('node')->clearRevisionsLanguage($language);
|
Chris@0
|
1389 }
|
Chris@0
|
1390
|
Chris@0
|
1391 /**
|
Chris@0
|
1392 * Marks a node to be re-indexed by the node_search plugin.
|
Chris@0
|
1393 *
|
Chris@0
|
1394 * @param int $nid
|
Chris@0
|
1395 * The node ID.
|
Chris@0
|
1396 */
|
Chris@0
|
1397 function node_reindex_node_search($nid) {
|
Chris@0
|
1398 if (\Drupal::moduleHandler()->moduleExists('search')) {
|
Chris@0
|
1399 // Reindex node context indexed by the node module search plugin.
|
Chris@0
|
1400 search_mark_for_reindex('node_search', $nid);
|
Chris@0
|
1401 }
|
Chris@0
|
1402 }
|
Chris@0
|
1403
|
Chris@0
|
1404 /**
|
Chris@0
|
1405 * Implements hook_ENTITY_TYPE_insert() for comment entities.
|
Chris@0
|
1406 */
|
Chris@0
|
1407 function node_comment_insert($comment) {
|
Chris@0
|
1408 // Reindex the node when comments are added.
|
Chris@0
|
1409 if ($comment->getCommentedEntityTypeId() == 'node') {
|
Chris@0
|
1410 node_reindex_node_search($comment->getCommentedEntityId());
|
Chris@0
|
1411 }
|
Chris@0
|
1412 }
|
Chris@0
|
1413
|
Chris@0
|
1414 /**
|
Chris@0
|
1415 * Implements hook_ENTITY_TYPE_update() for comment entities.
|
Chris@0
|
1416 */
|
Chris@0
|
1417 function node_comment_update($comment) {
|
Chris@0
|
1418 // Reindex the node when comments are changed.
|
Chris@0
|
1419 if ($comment->getCommentedEntityTypeId() == 'node') {
|
Chris@0
|
1420 node_reindex_node_search($comment->getCommentedEntityId());
|
Chris@0
|
1421 }
|
Chris@0
|
1422 }
|
Chris@0
|
1423
|
Chris@0
|
1424 /**
|
Chris@0
|
1425 * Implements hook_ENTITY_TYPE_delete() for comment entities.
|
Chris@0
|
1426 */
|
Chris@0
|
1427 function node_comment_delete($comment) {
|
Chris@0
|
1428 // Reindex the node when comments are deleted.
|
Chris@0
|
1429 if ($comment->getCommentedEntityTypeId() == 'node') {
|
Chris@0
|
1430 node_reindex_node_search($comment->getCommentedEntityId());
|
Chris@0
|
1431 }
|
Chris@0
|
1432 }
|
Chris@0
|
1433
|
Chris@0
|
1434 /**
|
Chris@0
|
1435 * Implements hook_config_translation_info_alter().
|
Chris@0
|
1436 */
|
Chris@0
|
1437 function node_config_translation_info_alter(&$info) {
|
Chris@0
|
1438 $info['node_type']['class'] = 'Drupal\node\ConfigTranslation\NodeTypeMapper';
|
Chris@0
|
1439 }
|