public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
- if (is_null($this->get('status')->value)) {
- if (\Drupal::currentUser()->hasPermission('skip comment approval')) {
- $this->setPublished();
- }
- else {
- $this->setUnpublished();
- }
- }
if ($this->isNew()) {
// Add the comment to database. This next section builds the thread field.
// @see \Drupal\comment\CommentViewBuilder::buildComponents()
$fields['langcode']->setDescription(t('The comment language code.'));
+ // Set the default value callback for the status field.
+ $fields['status']->setDefaultValueCallback('Drupal\comment\Entity\Comment::getDefaultStatus');
+
$fields['pid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Parent ID'))
->setDescription(t('The parent comment ID if this is a reply to a comment.'))
return $this->bundle();
}
+ /**
+ * Default value callback for 'status' base field definition.
+ *
+ * @see ::baseFieldDefinitions()
+ *
+ * @return bool
+ * TRUE if the comment should be published, FALSE otherwise.
+ */
+ public static function getDefaultStatus() {
+ return \Drupal::currentUser()->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED;
+ }
+
}