annotate core/modules/comment/src/Entity/Comment.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment\Entity;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Number;
Chris@0 6 use Drupal\Core\Cache\Cache;
Chris@0 7 use Drupal\Core\Entity\ContentEntityBase;
Chris@0 8 use Drupal\comment\CommentInterface;
Chris@0 9 use Drupal\Core\Entity\EntityChangedTrait;
Chris@0 10 use Drupal\Core\Entity\EntityPublishedTrait;
Chris@0 11 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 12 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 13 use Drupal\Core\Field\BaseFieldDefinition;
Chris@0 14 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 15 use Drupal\user\Entity\User;
Chris@0 16 use Drupal\user\UserInterface;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Defines the comment entity class.
Chris@0 20 *
Chris@0 21 * @ContentEntityType(
Chris@0 22 * id = "comment",
Chris@0 23 * label = @Translation("Comment"),
Chris@0 24 * label_singular = @Translation("comment"),
Chris@0 25 * label_plural = @Translation("comments"),
Chris@0 26 * label_count = @PluralTranslation(
Chris@0 27 * singular = "@count comment",
Chris@0 28 * plural = "@count comments",
Chris@0 29 * ),
Chris@0 30 * bundle_label = @Translation("Comment type"),
Chris@0 31 * handlers = {
Chris@0 32 * "storage" = "Drupal\comment\CommentStorage",
Chris@0 33 * "storage_schema" = "Drupal\comment\CommentStorageSchema",
Chris@0 34 * "access" = "Drupal\comment\CommentAccessControlHandler",
Chris@0 35 * "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
Chris@0 36 * "view_builder" = "Drupal\comment\CommentViewBuilder",
Chris@0 37 * "views_data" = "Drupal\comment\CommentViewsData",
Chris@0 38 * "form" = {
Chris@0 39 * "default" = "Drupal\comment\CommentForm",
Chris@0 40 * "delete" = "Drupal\comment\Form\DeleteForm"
Chris@0 41 * },
Chris@0 42 * "translation" = "Drupal\comment\CommentTranslationHandler"
Chris@0 43 * },
Chris@0 44 * base_table = "comment",
Chris@0 45 * data_table = "comment_field_data",
Chris@0 46 * uri_callback = "comment_uri",
Chris@0 47 * translatable = TRUE,
Chris@0 48 * entity_keys = {
Chris@0 49 * "id" = "cid",
Chris@0 50 * "bundle" = "comment_type",
Chris@0 51 * "label" = "subject",
Chris@0 52 * "langcode" = "langcode",
Chris@0 53 * "uuid" = "uuid",
Chris@0 54 * "published" = "status",
Chris@0 55 * },
Chris@0 56 * links = {
Chris@0 57 * "canonical" = "/comment/{comment}",
Chris@0 58 * "delete-form" = "/comment/{comment}/delete",
Chris@0 59 * "edit-form" = "/comment/{comment}/edit",
Chris@0 60 * "create" = "/comment",
Chris@0 61 * },
Chris@0 62 * bundle_entity_type = "comment_type",
Chris@0 63 * field_ui_base_route = "entity.comment_type.edit_form",
Chris@0 64 * constraints = {
Chris@0 65 * "CommentName" = {}
Chris@0 66 * }
Chris@0 67 * )
Chris@0 68 */
Chris@0 69 class Comment extends ContentEntityBase implements CommentInterface {
Chris@0 70
Chris@0 71 use EntityChangedTrait;
Chris@0 72 use EntityPublishedTrait;
Chris@0 73
Chris@0 74 /**
Chris@0 75 * The thread for which a lock was acquired.
Chris@0 76 *
Chris@0 77 * @var string
Chris@0 78 */
Chris@0 79 protected $threadLock = '';
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 public function preSave(EntityStorageInterface $storage) {
Chris@0 85 parent::preSave($storage);
Chris@0 86
Chris@0 87 if ($this->isNew()) {
Chris@0 88 // Add the comment to database. This next section builds the thread field.
Chris@0 89 // @see \Drupal\comment\CommentViewBuilder::buildComponents()
Chris@0 90 $thread = $this->getThread();
Chris@0 91 if (empty($thread)) {
Chris@0 92 if ($this->threadLock) {
Chris@0 93 // Thread lock was not released after being set previously.
Chris@0 94 // This suggests there's a bug in code using this class.
Chris@0 95 throw new \LogicException('preSave() is called again without calling postSave() or releaseThreadLock()');
Chris@0 96 }
Chris@0 97 if (!$this->hasParentComment()) {
Chris@0 98 // This is a comment with no parent comment (depth 0): we start
Chris@0 99 // by retrieving the maximum thread level.
Chris@0 100 $max = $storage->getMaxThread($this);
Chris@0 101 // Strip the "/" from the end of the thread.
Chris@0 102 $max = rtrim($max, '/');
Chris@0 103 // We need to get the value at the correct depth.
Chris@0 104 $parts = explode('.', $max);
Chris@0 105 $n = Number::alphadecimalToInt($parts[0]);
Chris@0 106 $prefix = '';
Chris@0 107 }
Chris@0 108 else {
Chris@0 109 // This is a comment with a parent comment, so increase the part of
Chris@0 110 // the thread value at the proper depth.
Chris@0 111
Chris@0 112 // Get the parent comment:
Chris@0 113 $parent = $this->getParentComment();
Chris@0 114 // Strip the "/" from the end of the parent thread.
Chris@0 115 $parent->setThread((string) rtrim((string) $parent->getThread(), '/'));
Chris@0 116 $prefix = $parent->getThread() . '.';
Chris@0 117 // Get the max value in *this* thread.
Chris@0 118 $max = $storage->getMaxThreadPerThread($this);
Chris@0 119
Chris@0 120 if ($max == '') {
Chris@0 121 // First child of this parent. As the other two cases do an
Chris@0 122 // increment of the thread number before creating the thread
Chris@0 123 // string set this to -1 so it requires an increment too.
Chris@0 124 $n = -1;
Chris@0 125 }
Chris@0 126 else {
Chris@0 127 // Strip the "/" at the end of the thread.
Chris@0 128 $max = rtrim($max, '/');
Chris@0 129 // Get the value at the correct depth.
Chris@0 130 $parts = explode('.', $max);
Chris@0 131 $parent_depth = count(explode('.', $parent->getThread()));
Chris@0 132 $n = Number::alphadecimalToInt($parts[$parent_depth]);
Chris@0 133 }
Chris@0 134 }
Chris@0 135 // Finally, build the thread field for this new comment. To avoid
Chris@0 136 // race conditions, get a lock on the thread. If another process already
Chris@0 137 // has the lock, just move to the next integer.
Chris@0 138 do {
Chris@0 139 $thread = $prefix . Number::intToAlphadecimal(++$n) . '/';
Chris@0 140 $lock_name = "comment:{$this->getCommentedEntityId()}:$thread";
Chris@0 141 } while (!\Drupal::lock()->acquire($lock_name));
Chris@0 142 $this->threadLock = $lock_name;
Chris@0 143 }
Chris@0 144 $this->setThread($thread);
Chris@0 145 if (!$this->getHostname()) {
Chris@0 146 // Ensure a client host from the current request.
Chris@0 147 $this->setHostname(\Drupal::request()->getClientIP());
Chris@0 148 }
Chris@0 149 }
Chris@0 150 // The entity fields for name and mail have no meaning if the user is not
Chris@0 151 // Anonymous. Set them to NULL to make it clearer that they are not used.
Chris@0 152 // For anonymous users see \Drupal\comment\CommentForm::form() for mail,
Chris@0 153 // and \Drupal\comment\CommentForm::buildEntity() for name setting.
Chris@0 154 if (!$this->getOwner()->isAnonymous()) {
Chris@0 155 $this->set('name', NULL);
Chris@0 156 $this->set('mail', NULL);
Chris@0 157 }
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
Chris@0 164 parent::postSave($storage, $update);
Chris@0 165
Chris@0 166 // Always invalidate the cache tag for the commented entity.
Chris@0 167 if ($commented_entity = $this->getCommentedEntity()) {
Chris@0 168 Cache::invalidateTags($commented_entity->getCacheTagsToInvalidate());
Chris@0 169 }
Chris@0 170
Chris@0 171 $this->releaseThreadLock();
Chris@0 172 // Update the {comment_entity_statistics} table prior to executing the hook.
Chris@0 173 \Drupal::service('comment.statistics')->update($this);
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Release the lock acquired for the thread in preSave().
Chris@0 178 */
Chris@0 179 protected function releaseThreadLock() {
Chris@0 180 if ($this->threadLock) {
Chris@0 181 \Drupal::lock()->release($this->threadLock);
Chris@0 182 $this->threadLock = '';
Chris@0 183 }
Chris@0 184 }
Chris@0 185
Chris@0 186 /**
Chris@0 187 * {@inheritdoc}
Chris@0 188 */
Chris@0 189 public static function postDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 190 parent::postDelete($storage, $entities);
Chris@0 191
Chris@0 192 $child_cids = $storage->getChildCids($entities);
Chris@0 193 entity_delete_multiple('comment', $child_cids);
Chris@0 194
Chris@0 195 foreach ($entities as $id => $entity) {
Chris@0 196 \Drupal::service('comment.statistics')->update($entity);
Chris@0 197 }
Chris@0 198 }
Chris@0 199
Chris@0 200 /**
Chris@0 201 * {@inheritdoc}
Chris@0 202 */
Chris@0 203 public function referencedEntities() {
Chris@0 204 $referenced_entities = parent::referencedEntities();
Chris@0 205 if ($this->getCommentedEntityId()) {
Chris@0 206 $referenced_entities[] = $this->getCommentedEntity();
Chris@0 207 }
Chris@0 208 return $referenced_entities;
Chris@0 209 }
Chris@0 210
Chris@0 211 /**
Chris@0 212 * {@inheritdoc}
Chris@0 213 */
Chris@0 214 public function permalink() {
Chris@0 215 $uri = $this->urlInfo();
Chris@0 216 $uri->setOption('fragment', 'comment-' . $this->id());
Chris@0 217 return $uri;
Chris@0 218 }
Chris@0 219
Chris@0 220 /**
Chris@0 221 * {@inheritdoc}
Chris@0 222 */
Chris@0 223 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
Chris@0 224 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
Chris@0 225 $fields = parent::baseFieldDefinitions($entity_type);
Chris@0 226 $fields += static::publishedBaseFieldDefinitions($entity_type);
Chris@0 227
Chris@0 228 $fields['cid']->setLabel(t('Comment ID'))
Chris@0 229 ->setDescription(t('The comment ID.'));
Chris@0 230
Chris@0 231 $fields['uuid']->setDescription(t('The comment UUID.'));
Chris@0 232
Chris@0 233 $fields['comment_type']->setLabel(t('Comment Type'))
Chris@0 234 ->setDescription(t('The comment type.'));
Chris@0 235
Chris@0 236 $fields['langcode']->setDescription(t('The comment language code.'));
Chris@0 237
Chris@0 238 // Set the default value callback for the status field.
Chris@0 239 $fields['status']->setDefaultValueCallback('Drupal\comment\Entity\Comment::getDefaultStatus');
Chris@0 240
Chris@0 241 $fields['pid'] = BaseFieldDefinition::create('entity_reference')
Chris@0 242 ->setLabel(t('Parent ID'))
Chris@0 243 ->setDescription(t('The parent comment ID if this is a reply to a comment.'))
Chris@0 244 ->setSetting('target_type', 'comment');
Chris@0 245
Chris@0 246 $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
Chris@0 247 ->setLabel(t('Entity ID'))
Chris@0 248 ->setDescription(t('The ID of the entity of which this comment is a reply.'))
Chris@0 249 ->setRequired(TRUE);
Chris@0 250
Chris@0 251 $fields['subject'] = BaseFieldDefinition::create('string')
Chris@0 252 ->setLabel(t('Subject'))
Chris@0 253 ->setTranslatable(TRUE)
Chris@0 254 ->setSetting('max_length', 64)
Chris@0 255 ->setDisplayOptions('form', [
Chris@0 256 'type' => 'string_textfield',
Chris@0 257 // Default comment body field has weight 20.
Chris@0 258 'weight' => 10,
Chris@0 259 ])
Chris@0 260 ->setDisplayConfigurable('form', TRUE);
Chris@0 261
Chris@0 262 $fields['uid'] = BaseFieldDefinition::create('entity_reference')
Chris@0 263 ->setLabel(t('User ID'))
Chris@0 264 ->setDescription(t('The user ID of the comment author.'))
Chris@0 265 ->setTranslatable(TRUE)
Chris@0 266 ->setSetting('target_type', 'user')
Chris@0 267 ->setDefaultValue(0);
Chris@0 268
Chris@0 269 $fields['name'] = BaseFieldDefinition::create('string')
Chris@0 270 ->setLabel(t('Name'))
Chris@0 271 ->setDescription(t("The comment author's name."))
Chris@0 272 ->setTranslatable(TRUE)
Chris@0 273 ->setSetting('max_length', 60)
Chris@0 274 ->setDefaultValue('');
Chris@0 275
Chris@0 276 $fields['mail'] = BaseFieldDefinition::create('email')
Chris@0 277 ->setLabel(t('Email'))
Chris@0 278 ->setDescription(t("The comment author's email address."))
Chris@0 279 ->setTranslatable(TRUE);
Chris@0 280
Chris@0 281 $fields['homepage'] = BaseFieldDefinition::create('uri')
Chris@0 282 ->setLabel(t('Homepage'))
Chris@0 283 ->setDescription(t("The comment author's home page address."))
Chris@0 284 ->setTranslatable(TRUE)
Chris@0 285 // URIs are not length limited by RFC 2616, but we can only store 255
Chris@0 286 // characters in our comment DB schema.
Chris@0 287 ->setSetting('max_length', 255);
Chris@0 288
Chris@0 289 $fields['hostname'] = BaseFieldDefinition::create('string')
Chris@0 290 ->setLabel(t('Hostname'))
Chris@0 291 ->setDescription(t("The comment author's hostname."))
Chris@0 292 ->setTranslatable(TRUE)
Chris@0 293 ->setSetting('max_length', 128);
Chris@0 294
Chris@0 295 $fields['created'] = BaseFieldDefinition::create('created')
Chris@0 296 ->setLabel(t('Created'))
Chris@0 297 ->setDescription(t('The time that the comment was created.'))
Chris@0 298 ->setTranslatable(TRUE);
Chris@0 299
Chris@0 300 $fields['changed'] = BaseFieldDefinition::create('changed')
Chris@0 301 ->setLabel(t('Changed'))
Chris@0 302 ->setDescription(t('The time that the comment was last edited.'))
Chris@0 303 ->setTranslatable(TRUE);
Chris@0 304
Chris@0 305 $fields['thread'] = BaseFieldDefinition::create('string')
Chris@0 306 ->setLabel(t('Thread place'))
Chris@0 307 ->setDescription(t("The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length."))
Chris@0 308 ->setSetting('max_length', 255);
Chris@0 309
Chris@0 310 $fields['entity_type'] = BaseFieldDefinition::create('string')
Chris@0 311 ->setLabel(t('Entity type'))
Chris@0 312 ->setDescription(t('The entity type to which this comment is attached.'))
Chris@0 313 ->setSetting('is_ascii', TRUE)
Chris@0 314 ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
Chris@0 315
Chris@0 316 $fields['field_name'] = BaseFieldDefinition::create('string')
Chris@0 317 ->setLabel(t('Comment field name'))
Chris@0 318 ->setDescription(t('The field name through which this comment was added.'))
Chris@0 319 ->setSetting('is_ascii', TRUE)
Chris@0 320 ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH);
Chris@0 321
Chris@0 322 return $fields;
Chris@0 323 }
Chris@0 324
Chris@0 325 /**
Chris@0 326 * {@inheritdoc}
Chris@0 327 */
Chris@0 328 public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
Chris@0 329 if ($comment_type = CommentType::load($bundle)) {
Chris@0 330 $fields['entity_id'] = clone $base_field_definitions['entity_id'];
Chris@0 331 $fields['entity_id']->setSetting('target_type', $comment_type->getTargetEntityTypeId());
Chris@0 332 return $fields;
Chris@0 333 }
Chris@0 334 return [];
Chris@0 335 }
Chris@0 336
Chris@0 337 /**
Chris@0 338 * {@inheritdoc}
Chris@0 339 */
Chris@0 340 public function hasParentComment() {
Chris@0 341 return (bool) $this->get('pid')->target_id;
Chris@0 342 }
Chris@0 343
Chris@0 344 /**
Chris@0 345 * {@inheritdoc}
Chris@0 346 */
Chris@0 347 public function getParentComment() {
Chris@0 348 return $this->get('pid')->entity;
Chris@0 349 }
Chris@0 350
Chris@0 351 /**
Chris@0 352 * {@inheritdoc}
Chris@0 353 */
Chris@0 354 public function getCommentedEntity() {
Chris@0 355 return $this->get('entity_id')->entity;
Chris@0 356 }
Chris@0 357
Chris@0 358 /**
Chris@0 359 * {@inheritdoc}
Chris@0 360 */
Chris@0 361 public function getCommentedEntityId() {
Chris@0 362 return $this->get('entity_id')->target_id;
Chris@0 363 }
Chris@0 364
Chris@0 365 /**
Chris@0 366 * {@inheritdoc}
Chris@0 367 */
Chris@0 368 public function getCommentedEntityTypeId() {
Chris@0 369 return $this->get('entity_type')->value;
Chris@0 370 }
Chris@0 371
Chris@0 372 /**
Chris@0 373 * {@inheritdoc}
Chris@0 374 */
Chris@0 375 public function setFieldName($field_name) {
Chris@0 376 $this->set('field_name', $field_name);
Chris@0 377 return $this;
Chris@0 378 }
Chris@0 379
Chris@0 380 /**
Chris@0 381 * {@inheritdoc}
Chris@0 382 */
Chris@0 383 public function getFieldName() {
Chris@0 384 return $this->get('field_name')->value;
Chris@0 385 }
Chris@0 386
Chris@0 387 /**
Chris@0 388 * {@inheritdoc}
Chris@0 389 */
Chris@0 390 public function getSubject() {
Chris@0 391 return $this->get('subject')->value;
Chris@0 392 }
Chris@0 393
Chris@0 394 /**
Chris@0 395 * {@inheritdoc}
Chris@0 396 */
Chris@0 397 public function setSubject($subject) {
Chris@0 398 $this->set('subject', $subject);
Chris@0 399 return $this;
Chris@0 400 }
Chris@0 401
Chris@0 402 /**
Chris@0 403 * {@inheritdoc}
Chris@0 404 */
Chris@0 405 public function getAuthorName() {
Chris@0 406 if ($this->get('uid')->target_id) {
Chris@0 407 return $this->get('uid')->entity->label();
Chris@0 408 }
Chris@0 409 return $this->get('name')->value ?: \Drupal::config('user.settings')->get('anonymous');
Chris@0 410 }
Chris@0 411
Chris@0 412 /**
Chris@0 413 * {@inheritdoc}
Chris@0 414 */
Chris@0 415 public function setAuthorName($name) {
Chris@0 416 $this->set('name', $name);
Chris@0 417 return $this;
Chris@0 418 }
Chris@0 419
Chris@0 420 /**
Chris@0 421 * {@inheritdoc}
Chris@0 422 */
Chris@0 423 public function getAuthorEmail() {
Chris@0 424 $mail = $this->get('mail')->value;
Chris@0 425
Chris@0 426 if ($this->get('uid')->target_id != 0) {
Chris@0 427 $mail = $this->get('uid')->entity->getEmail();
Chris@0 428 }
Chris@0 429
Chris@0 430 return $mail;
Chris@0 431 }
Chris@0 432
Chris@0 433 /**
Chris@0 434 * {@inheritdoc}
Chris@0 435 */
Chris@0 436 public function getHomepage() {
Chris@0 437 return $this->get('homepage')->value;
Chris@0 438 }
Chris@0 439
Chris@0 440 /**
Chris@0 441 * {@inheritdoc}
Chris@0 442 */
Chris@0 443 public function setHomepage($homepage) {
Chris@0 444 $this->set('homepage', $homepage);
Chris@0 445 return $this;
Chris@0 446 }
Chris@0 447
Chris@0 448 /**
Chris@0 449 * {@inheritdoc}
Chris@0 450 */
Chris@0 451 public function getHostname() {
Chris@0 452 return $this->get('hostname')->value;
Chris@0 453 }
Chris@0 454
Chris@0 455 /**
Chris@0 456 * {@inheritdoc}
Chris@0 457 */
Chris@0 458 public function setHostname($hostname) {
Chris@0 459 $this->set('hostname', $hostname);
Chris@0 460 return $this;
Chris@0 461 }
Chris@0 462
Chris@0 463 /**
Chris@0 464 * {@inheritdoc}
Chris@0 465 */
Chris@0 466 public function getCreatedTime() {
Chris@0 467 if (isset($this->get('created')->value)) {
Chris@0 468 return $this->get('created')->value;
Chris@0 469 }
Chris@0 470 return NULL;
Chris@0 471 }
Chris@0 472
Chris@0 473 /**
Chris@0 474 * {@inheritdoc}
Chris@0 475 */
Chris@0 476 public function setCreatedTime($created) {
Chris@0 477 $this->set('created', $created);
Chris@0 478 return $this;
Chris@0 479 }
Chris@0 480
Chris@0 481 /**
Chris@0 482 * {@inheritdoc}
Chris@0 483 */
Chris@0 484 public function getStatus() {
Chris@0 485 return $this->get('status')->value;
Chris@0 486 }
Chris@0 487
Chris@0 488 /**
Chris@0 489 * {@inheritdoc}
Chris@0 490 */
Chris@0 491 public function getThread() {
Chris@0 492 $thread = $this->get('thread');
Chris@0 493 if (!empty($thread->value)) {
Chris@0 494 return $thread->value;
Chris@0 495 }
Chris@0 496 }
Chris@0 497
Chris@0 498 /**
Chris@0 499 * {@inheritdoc}
Chris@0 500 */
Chris@0 501 public function setThread($thread) {
Chris@0 502 $this->set('thread', $thread);
Chris@0 503 return $this;
Chris@0 504 }
Chris@0 505
Chris@0 506 /**
Chris@0 507 * {@inheritdoc}
Chris@0 508 */
Chris@0 509 public static function preCreate(EntityStorageInterface $storage, array &$values) {
Chris@0 510 if (empty($values['comment_type']) && !empty($values['field_name']) && !empty($values['entity_type'])) {
Chris@0 511 $field_storage = FieldStorageConfig::loadByName($values['entity_type'], $values['field_name']);
Chris@0 512 $values['comment_type'] = $field_storage->getSetting('comment_type');
Chris@0 513 }
Chris@0 514 }
Chris@0 515
Chris@0 516 /**
Chris@0 517 * {@inheritdoc}
Chris@0 518 */
Chris@0 519 public function getOwner() {
Chris@0 520 $user = $this->get('uid')->entity;
Chris@0 521 if (!$user || $user->isAnonymous()) {
Chris@0 522 $user = User::getAnonymousUser();
Chris@0 523 $user->name = $this->getAuthorName();
Chris@0 524 $user->homepage = $this->getHomepage();
Chris@0 525 }
Chris@0 526 return $user;
Chris@0 527 }
Chris@0 528
Chris@0 529 /**
Chris@0 530 * {@inheritdoc}
Chris@0 531 */
Chris@0 532 public function getOwnerId() {
Chris@0 533 return $this->get('uid')->target_id;
Chris@0 534 }
Chris@0 535
Chris@0 536 /**
Chris@0 537 * {@inheritdoc}
Chris@0 538 */
Chris@0 539 public function setOwnerId($uid) {
Chris@0 540 $this->set('uid', $uid);
Chris@0 541 return $this;
Chris@0 542 }
Chris@0 543
Chris@0 544 /**
Chris@0 545 * {@inheritdoc}
Chris@0 546 */
Chris@0 547 public function setOwner(UserInterface $account) {
Chris@0 548 $this->set('uid', $account->id());
Chris@0 549 return $this;
Chris@0 550 }
Chris@0 551
Chris@0 552 /**
Chris@0 553 * Get the comment type ID for this comment.
Chris@0 554 *
Chris@0 555 * @return string
Chris@0 556 * The ID of the comment type.
Chris@0 557 */
Chris@0 558 public function getTypeId() {
Chris@0 559 return $this->bundle();
Chris@0 560 }
Chris@0 561
Chris@0 562 /**
Chris@0 563 * Default value callback for 'status' base field definition.
Chris@0 564 *
Chris@0 565 * @see ::baseFieldDefinitions()
Chris@0 566 *
Chris@0 567 * @return bool
Chris@0 568 * TRUE if the comment should be published, FALSE otherwise.
Chris@0 569 */
Chris@0 570 public static function getDefaultStatus() {
Chris@0 571 return \Drupal::currentUser()->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED;
Chris@0 572 }
Chris@0 573
Chris@0 574 }