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