3 namespace Drupal\node\Entity;
5 use Drupal\Core\Entity\EditorialContentEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\node\NodeInterface;
11 use Drupal\user\UserInterface;
14 * Defines the node entity class.
18 * label = @Translation("Content"),
19 * label_collection = @Translation("Content"),
20 * label_singular = @Translation("content item"),
21 * label_plural = @Translation("content items"),
22 * label_count = @PluralTranslation(
23 * singular = "@count content item",
24 * plural = "@count content items"
26 * bundle_label = @Translation("Content type"),
28 * "storage" = "Drupal\node\NodeStorage",
29 * "storage_schema" = "Drupal\node\NodeStorageSchema",
30 * "view_builder" = "Drupal\node\NodeViewBuilder",
31 * "access" = "Drupal\node\NodeAccessControlHandler",
32 * "views_data" = "Drupal\node\NodeViewsData",
34 * "default" = "Drupal\node\NodeForm",
35 * "delete" = "Drupal\node\Form\NodeDeleteForm",
36 * "edit" = "Drupal\node\NodeForm",
37 * "delete-multiple-confirm" = "Drupal\node\Form\DeleteMultiple"
39 * "route_provider" = {
40 * "html" = "Drupal\node\Entity\NodeRouteProvider",
42 * "list_builder" = "Drupal\node\NodeListBuilder",
43 * "translation" = "Drupal\node\NodeTranslationHandler"
45 * base_table = "node",
46 * data_table = "node_field_data",
47 * revision_table = "node_revision",
48 * revision_data_table = "node_field_revision",
49 * show_revision_ui = TRUE,
50 * translatable = TRUE,
51 * list_cache_contexts = { "user.node_grants:view" },
57 * "langcode" = "langcode",
59 * "status" = "status",
60 * "published" = "status",
63 * revision_metadata_keys = {
64 * "revision_user" = "revision_uid",
65 * "revision_created" = "revision_timestamp",
66 * "revision_log_message" = "revision_log"
68 * bundle_entity_type = "node_type",
69 * field_ui_base_route = "entity.node_type.edit_form",
70 * common_reference_target = TRUE,
71 * permission_granularity = "bundle",
73 * "canonical" = "/node/{node}",
74 * "delete-form" = "/node/{node}/delete",
75 * "delete-multiple-form" = "/admin/content/node/delete",
76 * "edit-form" = "/node/{node}/edit",
77 * "version-history" = "/node/{node}/revisions",
78 * "revision" = "/node/{node}/revisions/{node_revision}/view",
83 class Node extends EditorialContentEntityBase implements NodeInterface {
86 * Whether the node is being previewed or not.
88 * The variable is set to public as it will give a considerable performance
89 * improvement. See https://www.drupal.org/node/2498919.
92 * TRUE if the node is being previewed and NULL if it is not.
94 public $in_preview = NULL;
99 public function preSave(EntityStorageInterface $storage) {
100 parent::preSave($storage);
102 foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
103 $translation = $this->getTranslation($langcode);
105 // If no owner has been set explicitly, make the anonymous user the owner.
106 if (!$translation->getOwner()) {
107 $translation->setOwnerId(0);
111 // If no revision author has been set explicitly, make the node owner the
113 if (!$this->getRevisionUser()) {
114 $this->setRevisionUserId($this->getOwnerId());
121 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
122 parent::preSaveRevision($storage, $record);
124 if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
125 // If we are updating an existing node without adding a new revision, we
126 // need to make sure $entity->revision_log is reset whenever it is empty.
127 // Therefore, this code allows us to avoid clobbering an existing log
128 // entry with an empty one.
129 $record->revision_log = $this->original->revision_log->value;
136 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
137 parent::postSave($storage, $update);
139 // Update the node access table for this node, but only if it is the
140 // default revision. There's no need to delete existing records if the node
142 if ($this->isDefaultRevision()) {
143 /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
144 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
145 $grants = $access_control_handler->acquireGrants($this);
146 \Drupal::service('node.grant_storage')->write($this, $grants, NULL, $update);
149 // Reindex the node when it is updated. The node is automatically indexed
150 // when it is added, simply by being added to the node table.
152 node_reindex_node_search($this->id());
159 public static function preDelete(EntityStorageInterface $storage, array $entities) {
160 parent::preDelete($storage, $entities);
162 // Ensure that all nodes deleted are removed from the search index.
163 if (\Drupal::moduleHandler()->moduleExists('search')) {
164 foreach ($entities as $entity) {
165 search_index_clear('node_search', $entity->nid->value);
173 public static function postDelete(EntityStorageInterface $storage, array $nodes) {
174 parent::postDelete($storage, $nodes);
175 \Drupal::service('node.grant_storage')->deleteNodeRecords(array_keys($nodes));
181 public function getType() {
182 return $this->bundle();
188 public function access($operation = 'view', AccountInterface $account = NULL, $return_as_object = FALSE) {
189 // This override exists to set the operation to the default value "view".
190 return parent::access($operation, $account, $return_as_object);
196 public function getTitle() {
197 return $this->get('title')->value;
203 public function setTitle($title) {
204 $this->set('title', $title);
211 public function getCreatedTime() {
212 return $this->get('created')->value;
218 public function setCreatedTime($timestamp) {
219 $this->set('created', $timestamp);
226 public function isPromoted() {
227 return (bool) $this->get('promote')->value;
233 public function setPromoted($promoted) {
234 $this->set('promote', $promoted ? NodeInterface::PROMOTED : NodeInterface::NOT_PROMOTED);
241 public function isSticky() {
242 return (bool) $this->get('sticky')->value;
248 public function setSticky($sticky) {
249 $this->set('sticky', $sticky ? NodeInterface::STICKY : NodeInterface::NOT_STICKY);
256 public function getOwner() {
257 return $this->get('uid')->entity;
263 public function getOwnerId() {
264 return $this->getEntityKey('uid');
270 public function setOwnerId($uid) {
271 $this->set('uid', $uid);
278 public function setOwner(UserInterface $account) {
279 $this->set('uid', $account->id());
286 public function getRevisionAuthor() {
287 return $this->getRevisionUser();
293 public function setRevisionAuthorId($uid) {
294 $this->setRevisionUserId($uid);
301 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
302 $fields = parent::baseFieldDefinitions($entity_type);
304 $fields['title'] = BaseFieldDefinition::create('string')
305 ->setLabel(t('Title'))
307 ->setTranslatable(TRUE)
308 ->setRevisionable(TRUE)
309 ->setSetting('max_length', 255)
310 ->setDisplayOptions('view', [
315 ->setDisplayOptions('form', [
316 'type' => 'string_textfield',
319 ->setDisplayConfigurable('form', TRUE);
321 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
322 ->setLabel(t('Authored by'))
323 ->setDescription(t('The username of the content author.'))
324 ->setRevisionable(TRUE)
325 ->setSetting('target_type', 'user')
326 ->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
327 ->setTranslatable(TRUE)
328 ->setDisplayOptions('view', [
333 ->setDisplayOptions('form', [
334 'type' => 'entity_reference_autocomplete',
337 'match_operator' => 'CONTAINS',
342 ->setDisplayConfigurable('form', TRUE);
345 ->setDisplayOptions('form', [
346 'type' => 'boolean_checkbox',
348 'display_label' => TRUE,
352 ->setDisplayConfigurable('form', TRUE);
354 $fields['created'] = BaseFieldDefinition::create('created')
355 ->setLabel(t('Authored on'))
356 ->setDescription(t('The time that the node was created.'))
357 ->setRevisionable(TRUE)
358 ->setTranslatable(TRUE)
359 ->setDisplayOptions('view', [
361 'type' => 'timestamp',
364 ->setDisplayOptions('form', [
365 'type' => 'datetime_timestamp',
368 ->setDisplayConfigurable('form', TRUE);
370 $fields['changed'] = BaseFieldDefinition::create('changed')
371 ->setLabel(t('Changed'))
372 ->setDescription(t('The time that the node was last edited.'))
373 ->setRevisionable(TRUE)
374 ->setTranslatable(TRUE);
376 $fields['promote'] = BaseFieldDefinition::create('boolean')
377 ->setLabel(t('Promoted to front page'))
378 ->setRevisionable(TRUE)
379 ->setTranslatable(TRUE)
380 ->setDefaultValue(TRUE)
381 ->setDisplayOptions('form', [
382 'type' => 'boolean_checkbox',
384 'display_label' => TRUE,
388 ->setDisplayConfigurable('form', TRUE);
390 $fields['sticky'] = BaseFieldDefinition::create('boolean')
391 ->setLabel(t('Sticky at top of lists'))
392 ->setRevisionable(TRUE)
393 ->setTranslatable(TRUE)
394 ->setDefaultValue(FALSE)
395 ->setDisplayOptions('form', [
396 'type' => 'boolean_checkbox',
398 'display_label' => TRUE,
402 ->setDisplayConfigurable('form', TRUE);
408 * Default value callback for 'uid' base field definition.
410 * @see ::baseFieldDefinitions()
413 * An array of default values.
415 public static function getCurrentUserId() {
416 return [\Drupal::currentUser()->id()];