comparison core/modules/comment/src/Entity/Comment.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
11 use Drupal\Core\Entity\EntityStorageInterface; 11 use Drupal\Core\Entity\EntityStorageInterface;
12 use Drupal\Core\Entity\EntityTypeInterface; 12 use Drupal\Core\Entity\EntityTypeInterface;
13 use Drupal\Core\Field\BaseFieldDefinition; 13 use Drupal\Core\Field\BaseFieldDefinition;
14 use Drupal\field\Entity\FieldStorageConfig; 14 use Drupal\field\Entity\FieldStorageConfig;
15 use Drupal\user\Entity\User; 15 use Drupal\user\Entity\User;
16 use Drupal\user\UserInterface; 16 use Drupal\user\EntityOwnerTrait;
17 17
18 /** 18 /**
19 * Defines the comment entity class. 19 * Defines the comment entity class.
20 * 20 *
21 * @ContentEntityType( 21 * @ContentEntityType(
50 * "bundle" = "comment_type", 50 * "bundle" = "comment_type",
51 * "label" = "subject", 51 * "label" = "subject",
52 * "langcode" = "langcode", 52 * "langcode" = "langcode",
53 * "uuid" = "uuid", 53 * "uuid" = "uuid",
54 * "published" = "status", 54 * "published" = "status",
55 * "owner" = "uid",
55 * }, 56 * },
56 * links = { 57 * links = {
57 * "canonical" = "/comment/{comment}", 58 * "canonical" = "/comment/{comment}",
58 * "delete-form" = "/comment/{comment}/delete", 59 * "delete-form" = "/comment/{comment}/delete",
59 * "delete-multiple-form" = "/admin/content/comment/delete", 60 * "delete-multiple-form" = "/admin/content/comment/delete",
68 * ) 69 * )
69 */ 70 */
70 class Comment extends ContentEntityBase implements CommentInterface { 71 class Comment extends ContentEntityBase implements CommentInterface {
71 72
72 use EntityChangedTrait; 73 use EntityChangedTrait;
74 use EntityOwnerTrait;
73 use EntityPublishedTrait; 75 use EntityPublishedTrait;
74 76
75 /** 77 /**
76 * The thread for which a lock was acquired. 78 * The thread for which a lock was acquired.
77 * 79 *
207 209
208 /** 210 /**
209 * {@inheritdoc} 211 * {@inheritdoc}
210 */ 212 */
211 public function permalink() { 213 public function permalink() {
212 $uri = $this->urlInfo(); 214 $uri = $this->toUrl();
213 $uri->setOption('fragment', 'comment-' . $this->id()); 215 $uri->setOption('fragment', 'comment-' . $this->id());
214 return $uri; 216 return $uri;
215 } 217 }
216 218
217 /** 219 /**
219 */ 221 */
220 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { 222 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
221 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ 223 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
222 $fields = parent::baseFieldDefinitions($entity_type); 224 $fields = parent::baseFieldDefinitions($entity_type);
223 $fields += static::publishedBaseFieldDefinitions($entity_type); 225 $fields += static::publishedBaseFieldDefinitions($entity_type);
226 $fields += static::ownerBaseFieldDefinitions($entity_type);
224 227
225 $fields['cid']->setLabel(t('Comment ID')) 228 $fields['cid']->setLabel(t('Comment ID'))
226 ->setDescription(t('The comment ID.')); 229 ->setDescription(t('The comment ID.'));
227 230
228 $fields['uuid']->setDescription(t('The comment UUID.')); 231 $fields['uuid']->setDescription(t('The comment UUID.'));
254 // Default comment body field has weight 20. 257 // Default comment body field has weight 20.
255 'weight' => 10, 258 'weight' => 10,
256 ]) 259 ])
257 ->setDisplayConfigurable('form', TRUE); 260 ->setDisplayConfigurable('form', TRUE);
258 261
259 $fields['uid'] = BaseFieldDefinition::create('entity_reference') 262 $fields['uid']
260 ->setLabel(t('User ID')) 263 ->setDescription(t('The user ID of the comment author.'));
261 ->setDescription(t('The user ID of the comment author.'))
262 ->setTranslatable(TRUE)
263 ->setSetting('target_type', 'user')
264 ->setDefaultValue(0);
265 264
266 $fields['name'] = BaseFieldDefinition::create('string') 265 $fields['name'] = BaseFieldDefinition::create('string')
267 ->setLabel(t('Name')) 266 ->setLabel(t('Name'))
268 ->setDescription(t("The comment author's name.")) 267 ->setDescription(t("The comment author's name."))
269 ->setTranslatable(TRUE) 268 ->setTranslatable(TRUE)
305 ->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.")) 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."))
306 ->setSetting('max_length', 255); 305 ->setSetting('max_length', 255);
307 306
308 $fields['entity_type'] = BaseFieldDefinition::create('string') 307 $fields['entity_type'] = BaseFieldDefinition::create('string')
309 ->setLabel(t('Entity type')) 308 ->setLabel(t('Entity type'))
309 ->setRequired(TRUE)
310 ->setDescription(t('The entity type to which this comment is attached.')) 310 ->setDescription(t('The entity type to which this comment is attached.'))
311 ->setSetting('is_ascii', TRUE) 311 ->setSetting('is_ascii', TRUE)
312 ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH); 312 ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
313 313
314 $fields['field_name'] = BaseFieldDefinition::create('string') 314 $fields['field_name'] = BaseFieldDefinition::create('string')
315 ->setLabel(t('Comment field name')) 315 ->setLabel(t('Comment field name'))
316 ->setRequired(TRUE)
316 ->setDescription(t('The field name through which this comment was added.')) 317 ->setDescription(t('The field name through which this comment was added.'))
317 ->setSetting('is_ascii', TRUE) 318 ->setSetting('is_ascii', TRUE)
318 ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH); 319 ->setSetting('max_length', FieldStorageConfig::NAME_MAX_LENGTH);
319 320
320 return $fields; 321 return $fields;
322 }
323
324 /**
325 * {@inheritdoc}
326 */
327 public static function getDefaultEntityOwner() {
328 return 0;
321 } 329 }
322 330
323 /** 331 /**
324 * {@inheritdoc} 332 * {@inheritdoc}
325 */ 333 */
523 } 531 }
524 return $user; 532 return $user;
525 } 533 }
526 534
527 /** 535 /**
528 * {@inheritdoc}
529 */
530 public function getOwnerId() {
531 return $this->get('uid')->target_id;
532 }
533
534 /**
535 * {@inheritdoc}
536 */
537 public function setOwnerId($uid) {
538 $this->set('uid', $uid);
539 return $this;
540 }
541
542 /**
543 * {@inheritdoc}
544 */
545 public function setOwner(UserInterface $account) {
546 $this->set('uid', $account->id());
547 return $this;
548 }
549
550 /**
551 * Get the comment type ID for this comment. 536 * Get the comment type ID for this comment.
552 * 537 *
553 * @return string 538 * @return string
554 * The ID of the comment type. 539 * The ID of the comment type.
555 */ 540 */
574 * 559 *
575 * @return string 560 * @return string
576 * The client host name. 561 * The client host name.
577 */ 562 */
578 public static function getDefaultHostname() { 563 public static function getDefaultHostname() {
579 return \Drupal::request()->getClientIP(); 564 if (\Drupal::config('comment.settings')->get('log_ip_addresses')) {
565 return \Drupal::request()->getClientIP();
566 }
567 return '';
580 } 568 }
581 569
582 } 570 }