Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EditorialContentEntityBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
8 use Drupal\Core\Field\BaseFieldDefinition;
|
Chris@0
|
9 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
10 use Drupal\node\NodeInterface;
|
Chris@0
|
11 use Drupal\user\UserInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Defines the node entity class.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @ContentEntityType(
|
Chris@0
|
17 * id = "node",
|
Chris@0
|
18 * label = @Translation("Content"),
|
Chris@0
|
19 * label_collection = @Translation("Content"),
|
Chris@0
|
20 * label_singular = @Translation("content item"),
|
Chris@0
|
21 * label_plural = @Translation("content items"),
|
Chris@0
|
22 * label_count = @PluralTranslation(
|
Chris@0
|
23 * singular = "@count content item",
|
Chris@0
|
24 * plural = "@count content items"
|
Chris@0
|
25 * ),
|
Chris@0
|
26 * bundle_label = @Translation("Content type"),
|
Chris@0
|
27 * handlers = {
|
Chris@0
|
28 * "storage" = "Drupal\node\NodeStorage",
|
Chris@0
|
29 * "storage_schema" = "Drupal\node\NodeStorageSchema",
|
Chris@0
|
30 * "view_builder" = "Drupal\node\NodeViewBuilder",
|
Chris@0
|
31 * "access" = "Drupal\node\NodeAccessControlHandler",
|
Chris@0
|
32 * "views_data" = "Drupal\node\NodeViewsData",
|
Chris@0
|
33 * "form" = {
|
Chris@0
|
34 * "default" = "Drupal\node\NodeForm",
|
Chris@0
|
35 * "delete" = "Drupal\node\Form\NodeDeleteForm",
|
Chris@17
|
36 * "edit" = "Drupal\node\NodeForm",
|
Chris@17
|
37 * "delete-multiple-confirm" = "Drupal\node\Form\DeleteMultiple"
|
Chris@0
|
38 * },
|
Chris@0
|
39 * "route_provider" = {
|
Chris@0
|
40 * "html" = "Drupal\node\Entity\NodeRouteProvider",
|
Chris@0
|
41 * },
|
Chris@0
|
42 * "list_builder" = "Drupal\node\NodeListBuilder",
|
Chris@0
|
43 * "translation" = "Drupal\node\NodeTranslationHandler"
|
Chris@0
|
44 * },
|
Chris@0
|
45 * base_table = "node",
|
Chris@0
|
46 * data_table = "node_field_data",
|
Chris@0
|
47 * revision_table = "node_revision",
|
Chris@0
|
48 * revision_data_table = "node_field_revision",
|
Chris@0
|
49 * show_revision_ui = TRUE,
|
Chris@0
|
50 * translatable = TRUE,
|
Chris@0
|
51 * list_cache_contexts = { "user.node_grants:view" },
|
Chris@0
|
52 * entity_keys = {
|
Chris@0
|
53 * "id" = "nid",
|
Chris@0
|
54 * "revision" = "vid",
|
Chris@0
|
55 * "bundle" = "type",
|
Chris@0
|
56 * "label" = "title",
|
Chris@0
|
57 * "langcode" = "langcode",
|
Chris@0
|
58 * "uuid" = "uuid",
|
Chris@0
|
59 * "status" = "status",
|
Chris@0
|
60 * "published" = "status",
|
Chris@0
|
61 * "uid" = "uid",
|
Chris@0
|
62 * },
|
Chris@0
|
63 * revision_metadata_keys = {
|
Chris@0
|
64 * "revision_user" = "revision_uid",
|
Chris@0
|
65 * "revision_created" = "revision_timestamp",
|
Chris@0
|
66 * "revision_log_message" = "revision_log"
|
Chris@0
|
67 * },
|
Chris@0
|
68 * bundle_entity_type = "node_type",
|
Chris@0
|
69 * field_ui_base_route = "entity.node_type.edit_form",
|
Chris@0
|
70 * common_reference_target = TRUE,
|
Chris@0
|
71 * permission_granularity = "bundle",
|
Chris@0
|
72 * links = {
|
Chris@0
|
73 * "canonical" = "/node/{node}",
|
Chris@0
|
74 * "delete-form" = "/node/{node}/delete",
|
Chris@17
|
75 * "delete-multiple-form" = "/admin/content/node/delete",
|
Chris@0
|
76 * "edit-form" = "/node/{node}/edit",
|
Chris@0
|
77 * "version-history" = "/node/{node}/revisions",
|
Chris@0
|
78 * "revision" = "/node/{node}/revisions/{node_revision}/view",
|
Chris@0
|
79 * "create" = "/node",
|
Chris@0
|
80 * }
|
Chris@0
|
81 * )
|
Chris@0
|
82 */
|
Chris@0
|
83 class Node extends EditorialContentEntityBase implements NodeInterface {
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Whether the node is being previewed or not.
|
Chris@0
|
87 *
|
Chris@0
|
88 * The variable is set to public as it will give a considerable performance
|
Chris@0
|
89 * improvement. See https://www.drupal.org/node/2498919.
|
Chris@0
|
90 *
|
Chris@0
|
91 * @var true|null
|
Chris@0
|
92 * TRUE if the node is being previewed and NULL if it is not.
|
Chris@0
|
93 */
|
Chris@0
|
94 public $in_preview = NULL;
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@0
|
99 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
100 parent::preSave($storage);
|
Chris@0
|
101
|
Chris@0
|
102 foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
|
Chris@0
|
103 $translation = $this->getTranslation($langcode);
|
Chris@0
|
104
|
Chris@0
|
105 // If no owner has been set explicitly, make the anonymous user the owner.
|
Chris@0
|
106 if (!$translation->getOwner()) {
|
Chris@0
|
107 $translation->setOwnerId(0);
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 // If no revision author has been set explicitly, make the node owner the
|
Chris@0
|
112 // revision author.
|
Chris@0
|
113 if (!$this->getRevisionUser()) {
|
Chris@0
|
114 $this->setRevisionUserId($this->getOwnerId());
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
|
Chris@0
|
122 parent::preSaveRevision($storage, $record);
|
Chris@0
|
123
|
Chris@0
|
124 if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
|
Chris@0
|
125 // If we are updating an existing node without adding a new revision, we
|
Chris@0
|
126 // need to make sure $entity->revision_log is reset whenever it is empty.
|
Chris@0
|
127 // Therefore, this code allows us to avoid clobbering an existing log
|
Chris@0
|
128 // entry with an empty one.
|
Chris@0
|
129 $record->revision_log = $this->original->revision_log->value;
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * {@inheritdoc}
|
Chris@0
|
135 */
|
Chris@0
|
136 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
137 parent::postSave($storage, $update);
|
Chris@0
|
138
|
Chris@0
|
139 // Update the node access table for this node, but only if it is the
|
Chris@0
|
140 // default revision. There's no need to delete existing records if the node
|
Chris@0
|
141 // is new.
|
Chris@0
|
142 if ($this->isDefaultRevision()) {
|
Chris@0
|
143 /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
|
Chris@0
|
144 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
|
Chris@0
|
145 $grants = $access_control_handler->acquireGrants($this);
|
Chris@0
|
146 \Drupal::service('node.grant_storage')->write($this, $grants, NULL, $update);
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 // Reindex the node when it is updated. The node is automatically indexed
|
Chris@0
|
150 // when it is added, simply by being added to the node table.
|
Chris@0
|
151 if ($update) {
|
Chris@0
|
152 node_reindex_node_search($this->id());
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * {@inheritdoc}
|
Chris@0
|
158 */
|
Chris@0
|
159 public static function preDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
160 parent::preDelete($storage, $entities);
|
Chris@0
|
161
|
Chris@0
|
162 // Ensure that all nodes deleted are removed from the search index.
|
Chris@0
|
163 if (\Drupal::moduleHandler()->moduleExists('search')) {
|
Chris@0
|
164 foreach ($entities as $entity) {
|
Chris@0
|
165 search_index_clear('node_search', $entity->nid->value);
|
Chris@0
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * {@inheritdoc}
|
Chris@0
|
172 */
|
Chris@0
|
173 public static function postDelete(EntityStorageInterface $storage, array $nodes) {
|
Chris@0
|
174 parent::postDelete($storage, $nodes);
|
Chris@0
|
175 \Drupal::service('node.grant_storage')->deleteNodeRecords(array_keys($nodes));
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * {@inheritdoc}
|
Chris@0
|
180 */
|
Chris@0
|
181 public function getType() {
|
Chris@0
|
182 return $this->bundle();
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * {@inheritdoc}
|
Chris@0
|
187 */
|
Chris@0
|
188 public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
|
Chris@0
|
189 // This override exists to set the operation to the default value "view".
|
Chris@0
|
190 return parent::access($operation, $account, $return_as_object);
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 /**
|
Chris@0
|
194 * {@inheritdoc}
|
Chris@0
|
195 */
|
Chris@0
|
196 public function getTitle() {
|
Chris@0
|
197 return $this->get('title')->value;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * {@inheritdoc}
|
Chris@0
|
202 */
|
Chris@0
|
203 public function setTitle($title) {
|
Chris@0
|
204 $this->set('title', $title);
|
Chris@0
|
205 return $this;
|
Chris@0
|
206 }
|
Chris@0
|
207
|
Chris@0
|
208 /**
|
Chris@0
|
209 * {@inheritdoc}
|
Chris@0
|
210 */
|
Chris@0
|
211 public function getCreatedTime() {
|
Chris@0
|
212 return $this->get('created')->value;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * {@inheritdoc}
|
Chris@0
|
217 */
|
Chris@0
|
218 public function setCreatedTime($timestamp) {
|
Chris@0
|
219 $this->set('created', $timestamp);
|
Chris@0
|
220 return $this;
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 /**
|
Chris@0
|
224 * {@inheritdoc}
|
Chris@0
|
225 */
|
Chris@0
|
226 public function isPromoted() {
|
Chris@0
|
227 return (bool) $this->get('promote')->value;
|
Chris@0
|
228 }
|
Chris@0
|
229
|
Chris@0
|
230 /**
|
Chris@0
|
231 * {@inheritdoc}
|
Chris@0
|
232 */
|
Chris@0
|
233 public function setPromoted($promoted) {
|
Chris@0
|
234 $this->set('promote', $promoted ? NodeInterface::PROMOTED : NodeInterface::NOT_PROMOTED);
|
Chris@0
|
235 return $this;
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 /**
|
Chris@0
|
239 * {@inheritdoc}
|
Chris@0
|
240 */
|
Chris@0
|
241 public function isSticky() {
|
Chris@0
|
242 return (bool) $this->get('sticky')->value;
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * {@inheritdoc}
|
Chris@0
|
247 */
|
Chris@0
|
248 public function setSticky($sticky) {
|
Chris@0
|
249 $this->set('sticky', $sticky ? NodeInterface::STICKY : NodeInterface::NOT_STICKY);
|
Chris@0
|
250 return $this;
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 /**
|
Chris@0
|
254 * {@inheritdoc}
|
Chris@0
|
255 */
|
Chris@0
|
256 public function getOwner() {
|
Chris@0
|
257 return $this->get('uid')->entity;
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * {@inheritdoc}
|
Chris@0
|
262 */
|
Chris@0
|
263 public function getOwnerId() {
|
Chris@0
|
264 return $this->getEntityKey('uid');
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 /**
|
Chris@0
|
268 * {@inheritdoc}
|
Chris@0
|
269 */
|
Chris@0
|
270 public function setOwnerId($uid) {
|
Chris@0
|
271 $this->set('uid', $uid);
|
Chris@0
|
272 return $this;
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 /**
|
Chris@0
|
276 * {@inheritdoc}
|
Chris@0
|
277 */
|
Chris@0
|
278 public function setOwner(UserInterface $account) {
|
Chris@0
|
279 $this->set('uid', $account->id());
|
Chris@0
|
280 return $this;
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * {@inheritdoc}
|
Chris@0
|
285 */
|
Chris@0
|
286 public function getRevisionAuthor() {
|
Chris@0
|
287 return $this->getRevisionUser();
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 /**
|
Chris@0
|
291 * {@inheritdoc}
|
Chris@0
|
292 */
|
Chris@0
|
293 public function setRevisionAuthorId($uid) {
|
Chris@0
|
294 $this->setRevisionUserId($uid);
|
Chris@0
|
295 return $this;
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 /**
|
Chris@0
|
299 * {@inheritdoc}
|
Chris@0
|
300 */
|
Chris@0
|
301 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
Chris@0
|
302 $fields = parent::baseFieldDefinitions($entity_type);
|
Chris@0
|
303
|
Chris@0
|
304 $fields['title'] = BaseFieldDefinition::create('string')
|
Chris@0
|
305 ->setLabel(t('Title'))
|
Chris@0
|
306 ->setRequired(TRUE)
|
Chris@0
|
307 ->setTranslatable(TRUE)
|
Chris@0
|
308 ->setRevisionable(TRUE)
|
Chris@0
|
309 ->setSetting('max_length', 255)
|
Chris@0
|
310 ->setDisplayOptions('view', [
|
Chris@0
|
311 'label' => 'hidden',
|
Chris@0
|
312 'type' => 'string',
|
Chris@0
|
313 'weight' => -5,
|
Chris@0
|
314 ])
|
Chris@0
|
315 ->setDisplayOptions('form', [
|
Chris@0
|
316 'type' => 'string_textfield',
|
Chris@0
|
317 'weight' => -5,
|
Chris@0
|
318 ])
|
Chris@0
|
319 ->setDisplayConfigurable('form', TRUE);
|
Chris@0
|
320
|
Chris@0
|
321 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
|
Chris@0
|
322 ->setLabel(t('Authored by'))
|
Chris@0
|
323 ->setDescription(t('The username of the content author.'))
|
Chris@0
|
324 ->setRevisionable(TRUE)
|
Chris@0
|
325 ->setSetting('target_type', 'user')
|
Chris@0
|
326 ->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
|
Chris@0
|
327 ->setTranslatable(TRUE)
|
Chris@0
|
328 ->setDisplayOptions('view', [
|
Chris@0
|
329 'label' => 'hidden',
|
Chris@0
|
330 'type' => 'author',
|
Chris@0
|
331 'weight' => 0,
|
Chris@0
|
332 ])
|
Chris@0
|
333 ->setDisplayOptions('form', [
|
Chris@0
|
334 'type' => 'entity_reference_autocomplete',
|
Chris@0
|
335 'weight' => 5,
|
Chris@0
|
336 'settings' => [
|
Chris@0
|
337 'match_operator' => 'CONTAINS',
|
Chris@0
|
338 'size' => '60',
|
Chris@0
|
339 'placeholder' => '',
|
Chris@0
|
340 ],
|
Chris@0
|
341 ])
|
Chris@0
|
342 ->setDisplayConfigurable('form', TRUE);
|
Chris@0
|
343
|
Chris@0
|
344 $fields['status']
|
Chris@0
|
345 ->setDisplayOptions('form', [
|
Chris@0
|
346 'type' => 'boolean_checkbox',
|
Chris@0
|
347 'settings' => [
|
Chris@0
|
348 'display_label' => TRUE,
|
Chris@0
|
349 ],
|
Chris@0
|
350 'weight' => 120,
|
Chris@0
|
351 ])
|
Chris@0
|
352 ->setDisplayConfigurable('form', TRUE);
|
Chris@0
|
353
|
Chris@0
|
354 $fields['created'] = BaseFieldDefinition::create('created')
|
Chris@0
|
355 ->setLabel(t('Authored on'))
|
Chris@0
|
356 ->setDescription(t('The time that the node was created.'))
|
Chris@0
|
357 ->setRevisionable(TRUE)
|
Chris@0
|
358 ->setTranslatable(TRUE)
|
Chris@0
|
359 ->setDisplayOptions('view', [
|
Chris@0
|
360 'label' => 'hidden',
|
Chris@0
|
361 'type' => 'timestamp',
|
Chris@0
|
362 'weight' => 0,
|
Chris@0
|
363 ])
|
Chris@0
|
364 ->setDisplayOptions('form', [
|
Chris@0
|
365 'type' => 'datetime_timestamp',
|
Chris@0
|
366 'weight' => 10,
|
Chris@0
|
367 ])
|
Chris@0
|
368 ->setDisplayConfigurable('form', TRUE);
|
Chris@0
|
369
|
Chris@0
|
370 $fields['changed'] = BaseFieldDefinition::create('changed')
|
Chris@0
|
371 ->setLabel(t('Changed'))
|
Chris@0
|
372 ->setDescription(t('The time that the node was last edited.'))
|
Chris@0
|
373 ->setRevisionable(TRUE)
|
Chris@0
|
374 ->setTranslatable(TRUE);
|
Chris@0
|
375
|
Chris@0
|
376 $fields['promote'] = BaseFieldDefinition::create('boolean')
|
Chris@0
|
377 ->setLabel(t('Promoted to front page'))
|
Chris@0
|
378 ->setRevisionable(TRUE)
|
Chris@0
|
379 ->setTranslatable(TRUE)
|
Chris@0
|
380 ->setDefaultValue(TRUE)
|
Chris@0
|
381 ->setDisplayOptions('form', [
|
Chris@0
|
382 'type' => 'boolean_checkbox',
|
Chris@0
|
383 'settings' => [
|
Chris@0
|
384 'display_label' => TRUE,
|
Chris@0
|
385 ],
|
Chris@0
|
386 'weight' => 15,
|
Chris@0
|
387 ])
|
Chris@0
|
388 ->setDisplayConfigurable('form', TRUE);
|
Chris@0
|
389
|
Chris@0
|
390 $fields['sticky'] = BaseFieldDefinition::create('boolean')
|
Chris@0
|
391 ->setLabel(t('Sticky at top of lists'))
|
Chris@0
|
392 ->setRevisionable(TRUE)
|
Chris@0
|
393 ->setTranslatable(TRUE)
|
Chris@0
|
394 ->setDefaultValue(FALSE)
|
Chris@0
|
395 ->setDisplayOptions('form', [
|
Chris@0
|
396 'type' => 'boolean_checkbox',
|
Chris@0
|
397 'settings' => [
|
Chris@0
|
398 'display_label' => TRUE,
|
Chris@0
|
399 ],
|
Chris@0
|
400 'weight' => 16,
|
Chris@0
|
401 ])
|
Chris@0
|
402 ->setDisplayConfigurable('form', TRUE);
|
Chris@0
|
403
|
Chris@0
|
404 return $fields;
|
Chris@0
|
405 }
|
Chris@0
|
406
|
Chris@0
|
407 /**
|
Chris@0
|
408 * Default value callback for 'uid' base field definition.
|
Chris@0
|
409 *
|
Chris@0
|
410 * @see ::baseFieldDefinitions()
|
Chris@0
|
411 *
|
Chris@0
|
412 * @return array
|
Chris@0
|
413 * An array of default values.
|
Chris@0
|
414 */
|
Chris@0
|
415 public static function getCurrentUserId() {
|
Chris@0
|
416 return [\Drupal::currentUser()->id()];
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 }
|