diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index d34024b..b6483ee 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1053,8 +1053,9 @@ function comment_entity_insert(EntityInterface $entity) { if ($entity instanceof EntityAuthorInterface) { $last_comment_uid = $entity->getAuthorId(); } - else { - // Default to current user when entity does not have a uid property. + if (!isset($last_comment_uid)) { + // Default to current user when entity does not implements + // EntityAuthorInterface or author is not set. $last_comment_uid = \Drupal::currentUser()->id(); } // Default to REQUEST_TIME when entity does not have a changed property. diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index de039f0..9141048 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -98,6 +98,14 @@ public function updateEntityStatistics(CommentInterface $comment) { else { // Comments do not exist. $entity = entity_load($comment->entity_type->value, $comment->entity_id->value); + if ($entity instanceof EntityAuthorInterface) { + $last_comment_uid = $entity->getAuthorId(); + } + if (!isset($last_comment_uid)) { + // Default to current user when entity does not implements + // EntityAuthorInterface or author is not set. + $last_comment_uid = \Drupal::currentUser()->id(); + } $this->database->update('comment_entity_statistics') ->fields(array( 'cid' => 0, @@ -108,7 +116,7 @@ public function updateEntityStatistics(CommentInterface $comment) { 'last_comment_name' => '', // Get the user ID from the entity if it's set, or default to the // currently logged in user. - 'last_comment_uid' => $entity instanceof EntityAuthorInterface ? $entity->getAuthorId() : \Drupal::currentUser()->id(), + 'last_comment_uid' => $last_comment_uid, )) ->condition('entity_id', $comment->entity_id->value) ->condition('entity_type', $comment->entity_type->value) diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php index e158fe9..fc7e1e1 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Entity/EntityTest.php @@ -170,4 +170,5 @@ public function setAuthorId($uid) { $this->set('user_id', $uid); return $this; } + }