annotate core/modules/comment/src/Entity/Comment.php @ 19:fa3358dc1485 tip

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