diff -u b/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php --- b/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -140,19 +140,14 @@ } } - if ($build[$id]['#comment_threaded']) { - // The final comment must close up some hanging divs. - $build[$id]['#comment_indent_final'] = $current_indent; - } // Commented entities already loaded after self::getBuildDefaults(). $commented_entity = $entity->getCommentedEntity(); - if (!$commented_entity) { - return; - } + // Set defaults if the commented_entity does not exist. + $bundle = $commented_entity ? $commented_entity->bundle() : ''; + $entity_type_id = $commented_entity ? $commented_entity->getEntityTypeId() === 'node' : NULL; $build[$id]['#entity'] = $entity; - $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle(); - + $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $bundle; $display = $displays[$entity->bundle()]; if ($display->getComponent('links')) { $build[$id]['links'] = [ @@ -173,7 +168,7 @@ $build[$id]['#attached'] = []; } $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer'; - if ($attach_history && $commented_entity->getEntityTypeId() === 'node') { + if ($attach_history && $entity_type_id) { $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator'; // Embed the metadata for the comment "new" indicators on this node. @@ -183,6 +178,10 @@ ]; } } + if ($build[$id]['#comment_threaded']) { + // The final comment must close up some hanging divs. + $build[$id]['#comment_indent_final'] = $current_indent; + } } /** diff -u b/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php --- b/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -404,8 +404,9 @@ * {@inheritdoc} */ public function getAuthorName() { - if ($user = $this->get('uid')->entity) { - return $user->label(); + // If their is a valid user id and the user entity exists return the label. + if ($this->get('uid')->target_id && $this->get('uid')->entity) { + return $this->get('uid')->entity->label(); } return $this->get('name')->value ?: \Drupal::config('user.settings')->get('anonymous'); }