Node.php 12.5 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\node\Entity\Node.
6 7
 */

8
namespace Drupal\node\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\Core\Entity\EntityStorageInterface;
12
use Drupal\Core\Entity\EntityTypeInterface;
13
use Drupal\Core\Field\FieldDefinition;
14 15
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
16
use Drupal\node\NodeInterface;
17
use Drupal\user\UserInterface;
18 19 20

/**
 * Defines the node entity class.
21
 *
22
 * @ContentEntityType(
23
 *   id = "node",
24
 *   label = @Translation("Content"),
25
 *   bundle_label = @Translation("Content type"),
26
 *   controllers = {
27
 *     "view_builder" = "Drupal\node\NodeViewBuilder",
28 29
 *     "access" = "Drupal\node\NodeAccessController",
 *     "form" = {
30
 *       "default" = "Drupal\node\NodeFormController",
31
 *       "delete" = "Drupal\node\Form\NodeDeleteForm",
32
 *       "edit" = "Drupal\node\NodeFormController"
33
 *     },
34
 *     "list_builder" = "Drupal\node\NodeListBuilder",
35
 *     "translation" = "Drupal\node\NodeTranslationController"
36 37
 *   },
 *   base_table = "node",
38
 *   data_table = "node_field_data",
39 40
 *   revision_table = "node_revision",
 *   revision_data_table = "node_field_revision",
41 42
 *   uri_callback = "node_uri",
 *   fieldable = TRUE,
43
 *   translatable = TRUE,
44 45 46 47 48 49 50
 *   entity_keys = {
 *     "id" = "nid",
 *     "revision" = "vid",
 *     "bundle" = "type",
 *     "label" = "title",
 *     "uuid" = "uuid"
 *   },
51
 *   bundle_entity_type = "node_type",
52 53
 *   permission_granularity = "bundle",
 *   links = {
54
 *     "canonical" = "node.view",
55
 *     "delete-form" = "node.delete_confirm",
56
 *     "edit-form" = "node.page_edit",
57 58
 *     "version-history" = "node.revision_overview",
 *     "admin-form" = "node.type_edit"
59
 *   }
60
 * )
61
 */
62
class Node extends ContentEntityBase implements NodeInterface {
63 64

  /**
65
   * Implements Drupal\Core\Entity\EntityInterface::id().
66
   */
67 68 69
  public function id() {
    return $this->get('nid')->value;
  }
70

71
  /**
72
   * Overrides Drupal\Core\Entity\Entity::getRevisionId().
73
   */
74 75 76
  public function getRevisionId() {
    return $this->get('vid')->value;
  }
77

78
  /**
79
   * {@inheritdoc}
80
   */
81 82
  public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
    parent::preSaveRevision($storage, $record);
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    if ($this->newRevision) {
      // When inserting either a new node or a new node revision, $node->log
      // must be set because {node_field_revision}.log is a text column and
      // therefore cannot have a default value. However, it might not be set at
      // this point (for example, if the user submitting a node form does not
      // have permission to create revisions), so we ensure that it is at least
      // an empty string in that case.
      // @todo Make the {node_field_revision}.log column nullable so that we
      //   can remove this check.
      if (!isset($record->log)) {
        $record->log = '';
      }
    }
    elseif (isset($this->original) && (!isset($record->log) || $record->log === '')) {
      // If we are updating an existing node without adding a new revision, we
      // need to make sure $entity->log is reset whenever it is empty.
      // Therefore, this code allows us to avoid clobbering an existing log
      // entry with an empty one.
102
      $record->log = $this->original->log->value;
103 104
    }
  }
105 106

  /**
107
   * {@inheritdoc}
108
   */
109 110
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);
111

112 113 114 115
    // Update the node access table for this node, but only if it is the
    // default revision. There's no need to delete existing records if the node
    // is new.
    if ($this->isDefaultRevision()) {
116
      \Drupal::entityManager()->getAccessController('node')->writeGrants($this, $update);
117
    }
118 119 120 121 122 123

    // Reindex the node when it is updated. The node is automatically indexed
    // when it is added, simply by being added to the node table.
    if ($update) {
      node_reindex_node_search($this->id());
    }
124
  }
125 126

  /**
127
   * {@inheritdoc}
128
   */
129 130
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage, $entities);
131

132 133
    // Assure that all nodes deleted are removed from the search index.
    if (\Drupal::moduleHandler()->moduleExists('search')) {
134
      foreach ($entities as $entity) {
135
        search_reindex($entity->nid->value, 'node_search');
136 137 138
      }
    }
  }
139

140 141 142
  /**
   * {@inheritdoc}
   */
143 144
  public static function postDelete(EntityStorageInterface $storage, array $nodes) {
    parent::postDelete($storage, $nodes);
145 146 147
    \Drupal::service('node.grant_storage')->deleteNodeRecords(array_keys($nodes));
  }

148 149 150 151 152 153 154
  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this->bundle();
  }

155 156 157 158 159 160 161 162 163
  /**
   * {@inheritdoc}
   */
  public function access($operation = 'view', AccountInterface $account = NULL) {
    if ($operation == 'create') {
      return parent::access($operation, $account);
    }

    return \Drupal::entityManager()
164
      ->getAccessController($this->entityTypeId)
165 166 167 168 169 170 171 172 173 174 175 176 177 178
      ->access($this, $operation, $this->prepareLangcode(), $account);
  }

  /**
   * {@inheritdoc}
   */
  public function prepareLangcode() {
    $langcode = $this->language()->id;
    // If the Language module is enabled, try to use the language from content
    // negotiation.
    if (\Drupal::moduleHandler()->moduleExists('language')) {
      // Load languages the node exists in.
      $node_translations = $this->getTranslationLanguages();
      // Load the language from content negotiation.
179
      $content_negotiation_langcode = \Drupal::languageManager()->getCurrentLanguage(Language::TYPE_CONTENT)->id;
180 181 182 183 184 185 186 187 188
      // If there is a translation available, use it.
      if (isset($node_translations[$content_negotiation_langcode])) {
        $langcode = $content_negotiation_langcode;
      }
    }
    return $langcode;
  }


189 190 191 192 193 194 195
  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this->get('title')->value;
  }

196
  /**
197
   * {@inheritdoc}
198
   */
199 200 201 202
  public function setTitle($title) {
    $this->set('title', $title);
    return $this;
  }
203 204

  /**
205
   * {@inheritdoc}
206
   */
207 208 209
  public function getCreatedTime() {
    return $this->get('created')->value;
  }
210 211 212


  /**
213
   * {@inheritdoc}
214
   */
215 216 217 218
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }
219 220

  /**
221
   * {@inheritdoc}
222
   */
223 224 225
  public function getChangedTime() {
    return $this->get('changed')->value;
  }
226 227

  /**
228
   * {@inheritdoc}
229
   */
230 231 232
  public function isPromoted() {
    return (bool) $this->get('promote')->value;
  }
233 234

  /**
235
   * {@inheritdoc}
236
   */
237
  public function setPromoted($promoted) {
238
    $this->set('promote', $promoted ? NODE_PROMOTED : NODE_NOT_PROMOTED);
239 240
    return $this;
  }
241 242

  /**
243
   * {@inheritdoc}
244
   */
245 246 247
  public function isSticky() {
    return (bool) $this->get('sticky')->value;
  }
248 249

  /**
250
   * {@inheritdoc}
251
   */
252 253 254 255
  public function setSticky($sticky) {
    $this->set('sticky', $sticky ? NODE_STICKY : NODE_NOT_STICKY);
    return $this;
  }
256
  /**
257
   * {@inheritdoc}
258
   */
259 260 261
  public function isPublished() {
    return (bool) $this->get('status')->value;
  }
262

263
  /**
264
   * {@inheritdoc}
265
   */
266 267 268
  public function setPublished($published) {
    $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
    return $this;
269 270 271
  }

  /**
272
   * {@inheritdoc}
273
   */
274
  public function getOwner() {
275
    return $this->get('uid')->entity;
276
  }
277 278

  /**
279
   * {@inheritdoc}
280
   */
281
  public function getOwnerId() {
282
    return $this->get('uid')->target_id;
283
  }
284

285 286 287
  /**
   * {@inheritdoc}
   */
288
  public function setOwnerId($uid) {
289 290
    $this->set('uid', $uid);
    return $this;
291 292
  }

293 294 295 296 297 298 299 300
  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this->set('uid', $account->id());
    return $this;
  }

301 302 303
  /**
   * {@inheritdoc}
   */
304 305
  public function getRevisionCreationTime() {
    return $this->get('revision_timestamp')->value;
306 307 308 309 310
  }

  /**
   * {@inheritdoc}
   */
311 312 313
  public function setRevisionCreationTime($timestamp) {
    $this->set('revision_timestamp', $timestamp);
    return $this;
314 315
  }

316 317 318
  /**
   * {@inheritdoc}
   */
319 320
  public function getRevisionAuthor() {
    return $this->get('revision_uid')->entity;
321 322
  }

323 324 325
  /**
   * {@inheritdoc}
   */
326 327 328
  public function setRevisionAuthorId($uid) {
    $this->set('revision_uid', $uid);
    return $this;
329 330
  }

331 332 333
  /**
   * {@inheritdoc}
   */
334
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
335 336 337
    $fields['nid'] = FieldDefinition::create('integer')
      ->setLabel(t('Node ID'))
      ->setDescription(t('The node ID.'))
338 339
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
340 341 342 343 344 345 346 347 348

    $fields['uuid'] = FieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The node UUID.'))
      ->setReadOnly(TRUE);

    $fields['vid'] = FieldDefinition::create('integer')
      ->setLabel(t('Revision ID'))
      ->setDescription(t('The node revision ID.'))
349 350
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
351 352 353 354

    $fields['type'] = FieldDefinition::create('entity_reference')
      ->setLabel(t('Type'))
      ->setDescription(t('The node type.'))
355
      ->setSetting('target_type', 'node_type')
356 357 358 359
      ->setReadOnly(TRUE);

    $fields['langcode'] = FieldDefinition::create('language')
      ->setLabel(t('Language code'))
360 361
      ->setDescription(t('The node language code.'))
      ->setRevisionable(TRUE);
362

363
    $fields['title'] = FieldDefinition::create('string')
364 365 366 367
      ->setLabel(t('Title'))
      ->setDescription(t('The title of this node, always treated as non-markup plain text.'))
      ->setRequired(TRUE)
      ->setTranslatable(TRUE)
368
      ->setRevisionable(TRUE)
369
      ->setSettings(array(
370
        'default_value' => '',
371
        'max_length' => 255,
372 373 374
      ))
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
375
        'type' => 'string',
376 377 378
        'weight' => -5,
      ))
      ->setDisplayOptions('form', array(
379
        'type' => 'string',
380 381 382
        'weight' => -5,
      ))
      ->setDisplayConfigurable('form', TRUE);
383 384

    $fields['uid'] = FieldDefinition::create('entity_reference')
385 386
      ->setLabel(t('Author'))
      ->setDescription(t('The user that is the node author.'))
387
      ->setRevisionable(TRUE)
388
      ->setSettings(array(
389 390
        'target_type' => 'user',
        'default_value' => 0,
391 392
      ))
      ->setTranslatable(TRUE);
393 394 395

    $fields['status'] = FieldDefinition::create('boolean')
      ->setLabel(t('Publishing status'))
396
      ->setDescription(t('A boolean indicating whether the node is published.'))
397 398
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
399

400
    $fields['created'] = FieldDefinition::create('created')
401
      ->setLabel(t('Created'))
402
      ->setDescription(t('The time that the node was created.'))
403 404
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
405

406
    $fields['changed'] = FieldDefinition::create('changed')
407
      ->setLabel(t('Changed'))
408
      ->setDescription(t('The time that the node was last edited.'))
409 410
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
411 412 413

    $fields['promote'] = FieldDefinition::create('boolean')
      ->setLabel(t('Promote'))
414
      ->setDescription(t('A boolean indicating whether the node should be displayed on the front page.'))
415 416
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
417 418 419

    $fields['sticky'] = FieldDefinition::create('boolean')
      ->setLabel(t('Sticky'))
420
      ->setDescription(t('A boolean indicating whether the node should be displayed at the top of lists in which it appears.'))
421 422
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
423

424
    $fields['revision_timestamp'] = FieldDefinition::create('timestamp')
425 426
      ->setLabel(t('Revision timestamp'))
      ->setDescription(t('The time that the current revision was created.'))
427 428
      ->setQueryable(FALSE)
      ->setRevisionable(TRUE);
429 430 431 432

    $fields['revision_uid'] = FieldDefinition::create('entity_reference')
      ->setLabel(t('Revision user ID'))
      ->setDescription(t('The user ID of the author of the current revision.'))
433
      ->setSettings(array('target_type' => 'user'))
434 435
      ->setQueryable(FALSE)
      ->setRevisionable(TRUE);
436 437 438

    $fields['log'] = FieldDefinition::create('string')
      ->setLabel(t('Log'))
439
      ->setDescription(t('The log entry explaining the changes in this revision.'))
440 441
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
442 443

    return $fields;
444 445
  }

446 447 448 449 450 451 452 453 454 455 456 457 458
  /**
   * {@inheritdoc}
   */
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
    $node_type = node_type_load($bundle);
    $fields = array();
    if (isset($node_type->title_label)) {
      $fields['title'] = clone $base_field_definitions['title'];
      $fields['title']->setLabel($node_type->title_label);
    }
    return $fields;
  }

459
}