Node.php 6.66 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\EntityNG;
11
use Drupal\Core\Entity\EntityStorageControllerInterface;
12
use Drupal\Core\Entity\Annotation\EntityType;
13
use Drupal\Core\Annotation\Translation;
14
use Drupal\node\NodeInterface;
15
use Drupal\node\NodeBCDecorator;
16 17 18

/**
 * Defines the node entity class.
19
 *
20
 * @EntityType(
21
 *   id = "node",
22
 *   label = @Translation("Content"),
23
 *   bundle_label = @Translation("Content type"),
24
 *   module = "node",
25 26 27 28 29
 *   controllers = {
 *     "storage" = "Drupal\node\NodeStorageController",
 *     "render" = "Drupal\node\NodeRenderController",
 *     "access" = "Drupal\node\NodeAccessController",
 *     "form" = {
30
 *       "default" = "Drupal\node\NodeFormController",
31
 *       "delete" = "Drupal\node\Form\NodeDeleteForm",
32
 *       "edit" = "Drupal\node\NodeFormController"
33 34
 *     },
 *     "translation" = "Drupal\node\NodeTranslationController"
35 36
 *   },
 *   base_table = "node",
37 38
 *   data_table = "node_field_data",
 *   revision_table = "node_field_revision",
39 40
 *   uri_callback = "node_uri",
 *   fieldable = TRUE,
41
 *   translatable = TRUE,
42 43 44 45 46 47 48 49 50
 *   entity_keys = {
 *     "id" = "nid",
 *     "revision" = "vid",
 *     "bundle" = "type",
 *     "label" = "title",
 *     "uuid" = "uuid"
 *   },
 *   bundle_keys = {
 *     "bundle" = "type"
51
 *   },
52
 *   route_base_path = "admin/structure/types/manage/{bundle}",
53 54 55 56 57 58
 *   permission_granularity = "bundle",
 *   links = {
 *     "canonical" = "/node/{node}",
 *     "edit-form" = "/node/{node}/edit",
 *     "version-history" = "/node/{node}/revisions"
 *   }
59
 * )
60
 */
61
class Node extends EntityNG implements NodeInterface {
62 63

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

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

77
  /**
78
   * {@inheritdoc}
79
   */
80 81 82 83
  public function preSave(EntityStorageControllerInterface $storage_controller) {
    // Before saving the node, set changed and revision times.
    $this->changed->value = REQUEST_TIME;
  }
84 85

  /**
86
   * {@inheritdoc}
87
   */
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  public function preSaveRevision(EntityStorageControllerInterface $storage_controller, \stdClass $record) {
    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.
      $record->log = $this->original->log;
    }
  }
110 111

  /**
112
   * {@inheritdoc}
113
   */
114 115 116 117 118
  public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
    // 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()) {
119
      \Drupal::entityManager()->getAccessController('node')->writeGrants($this->getBCEntity(), $update);
120 121
    }
  }
122 123

  /**
124
   * {@inheritdoc}
125
   */
126 127 128 129 130 131 132
  public function getBCEntity() {
    if (!isset($this->bcEntity)) {
      $this->getPropertyDefinitions();
      $this->bcEntity = new NodeBCDecorator($this, $this->fieldDefinitions);
    }
    return $this->bcEntity;
  }
133 134

  /**
135
   * {@inheritdoc}
136
   */
137 138
  public static function preDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
    if (module_exists('search')) {
139
      foreach ($entities as $entity) {
140 141 142 143
        search_reindex($entity->nid->value, 'node');
      }
    }
  }
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158
  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this->bundle();
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this->get('title')->value;
  }

159
  /**
160
   * {@inheritdoc}
161
   */
162 163 164 165
  public function setTitle($title) {
    $this->set('title', $title);
    return $this;
  }
166 167

  /**
168
   * {@inheritdoc}
169
   */
170 171 172
  public function getCreatedTime() {
    return $this->get('created')->value;
  }
173 174 175


  /**
176
   * {@inheritdoc}
177
   */
178 179 180 181
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }
182 183

  /**
184
   * {@inheritdoc}
185
   */
186 187 188
  public function getChangedTime() {
    return $this->get('changed')->value;
  }
189 190

  /**
191
   * {@inheritdoc}
192
   */
193 194 195
  public function isPromoted() {
    return (bool) $this->get('promote')->value;
  }
196 197

  /**
198
   * {@inheritdoc}
199
   */
200
  public function setPromoted($promoted) {
201
    $this->set('promote', $promoted ? NODE_PROMOTED : NODE_NOT_PROMOTED);
202 203
    return $this;
  }
204 205

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

  /**
213
   * {@inheritdoc}
214
   */
215 216 217 218
  public function setSticky($sticky) {
    $this->set('sticky', $sticky ? NODE_STICKY : NODE_NOT_STICKY);
    return $this;
  }
219
  /**
220
   * {@inheritdoc}
221
   */
222 223 224
  public function isPublished() {
    return (bool) $this->get('status')->value;
  }
225

226
  /**
227
   * {@inheritdoc}
228
   */
229 230 231
  public function setPublished($published) {
    $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
    return $this;
232 233 234
  }

  /**
235
   * {@inheritdoc}
236
   */
237
  public function getAuthor() {
238
    return $this->get('uid')->entity;
239
  }
240 241

  /**
242
   * {@inheritdoc}
243
   */
244 245
  public function getAuthorId() {
    return $this->get('uid')->target_id;
246
  }
247

248 249 250
  /**
   * {@inheritdoc}
   */
251 252 253
  public function setAuthorId($uid) {
    $this->set('uid', $uid);
    return $this;
254 255 256 257 258
  }

  /**
   * {@inheritdoc}
   */
259 260
  public function getRevisionCreationTime() {
    return $this->get('revision_timestamp')->value;
261 262 263 264 265
  }

  /**
   * {@inheritdoc}
   */
266 267 268
  public function setRevisionCreationTime($timestamp) {
    $this->set('revision_timestamp', $timestamp);
    return $this;
269 270
  }

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

278 279 280
  /**
   * {@inheritdoc}
   */
281 282 283
  public function setRevisionAuthorId($uid) {
    $this->set('revision_uid', $uid);
    return $this;
284 285
  }

286
}