Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / comment / src / Entity / Comment.php
index a5f196646974da48c9e71ba4b35f1cc12d338630..8ce0c73edb9efd967d04e34160155edd6330c718 100644 (file)
@@ -81,14 +81,6 @@ class Comment extends ContentEntityBase implements CommentInterface {
   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()
@@ -237,6 +229,9 @@ class Comment extends ContentEntityBase implements CommentInterface {
 
     $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.'))
@@ -558,4 +553,16 @@ class Comment extends ContentEntityBase implements CommentInterface {
     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;
+  }
+
 }