reverted: --- b/core/modules/comment/comment.module +++ a/core/modules/comment/comment.module @@ -503,14 +503,14 @@ $preview_build['comment_preview'] = $comment_build; } - $build = []; if ($comment->hasParentComment()) { + $build = []; $parent = $comment->getParentComment(); if ($parent && $parent->isPublished()) { $build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($parent); } } + else { - elseif ($entity) { // The comment field output includes rendering the parent entity of the // thread to which the comment is a reply. The rendered entity output // includes the comment reply form, which contains the comment preview and @@ -607,8 +607,9 @@ $variables['submitted'] = t('Submitted by @username on @datetime', ['@username' => $variables['author'], '@datetime' => $variables['created']]); + if ($comment->hasParentComment()) { - if ($comment_parent = $comment->getParentComment()) { // Fetch and store the parent comment information for use in templates. + $comment_parent = $comment->getParentComment(); $account_parent = $comment_parent->getOwner(); $variables['parent_comment'] = $comment_parent; $username = [ reverted: --- b/core/modules/comment/comment.tokens.inc +++ a/core/modules/comment/comment.tokens.inc @@ -124,11 +124,13 @@ $token_service = \Drupal::token(); $url_options = ['absolute' => TRUE]; - $langcode = NULL; if (isset($options['langcode'])) { $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); $langcode = $options['langcode']; } + else { + $langcode = NULL; + } $replacements = []; if ($type == 'comment' && !empty($data['comment'])) { @@ -198,11 +200,9 @@ case 'parent': if ($comment->hasParentComment()) { + $parent = $comment->getParentComment(); + $bubbleable_metadata->addCacheableDependency($parent); + $replacements[$original] = $parent->getSubject(); - $replacements[$original] = '[not found]'; - if ($parent = $comment->getParentComment()) { - $bubbleable_metadata->addCacheableDependency($parent); - $replacements[$original] = $parent->getSubject(); - } } break; @@ -219,12 +219,10 @@ break; case 'entity': + $entity = $comment->getCommentedEntity(); + $bubbleable_metadata->addCacheableDependency($entity); + $title = $entity->label(); + $replacements[$original] = $title; - $replacements[$original] = '[not found]'; - if ($entity = $comment->getCommentedEntity()) { - $bubbleable_metadata->addCacheableDependency($entity); - $title = $entity->label(); - $replacements[$original] = $title; - } break; } } reverted: --- b/core/modules/comment/src/CommentInterface.php +++ a/core/modules/comment/src/CommentInterface.php @@ -40,10 +40,6 @@ /** * Determines if this comment is a reply to another comment. * - * Calling getParentComment() without checking hasParentComment() first, is - * allowed. hasParentComment() is generally faster but can still return a - * value even when the parent comment cannot be loaded. - * * @return bool * TRUE if the comment has a parent comment otherwise FALSE. */ @@ -53,8 +49,7 @@ * Returns the parent comment entity if this is a reply to a comment. * * @return \Drupal\comment\CommentInterface|null + * A comment entity of the parent comment or NULL if there is no parent. - * A comment entity of the parent comment; NULL if there is no parent or - * if the parent somehow cannot be loaded. */ public function getParentComment(); @@ -70,10 +65,6 @@ /** * Returns the ID of the entity to which the comment is attached. * - * Calling getCommentedEntity() without checking getCommentedEntityId() first, - * is allowed. getCommentedEntityId() is generally faster but can still return - * a value even when the commented entity cannot be loaded. - * * @return int * The ID of the entity to which the comment is attached. */ reverted: --- b/core/modules/comment/src/CommentViewBuilder.php +++ a/core/modules/comment/src/CommentViewBuilder.php @@ -80,11 +80,9 @@ /** @var \Drupal\comment\CommentInterface $entity */ // Store a threading field setting to use later in self::buildComponents(). + $build['#comment_threaded'] = $entity->getCommentedEntity() + ->getFieldDefinition($entity->getFieldName()) + ->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED; - $commented_entity = $entity->getCommentedEntity(); - $build['#comment_threaded'] = - !$commented_entity - || $commented_entity->getFieldDefinition($entity->getFieldName()) - ->getSetting('default_mode') === CommentManagerInterface::COMMENT_MODE_THREADED; // If threading is enabled, don't render cache individual comments, but do // keep the cacheability metadata, so it can bubble up. if ($build['#comment_threaded']) { @@ -144,7 +142,8 @@ $commented_entity = $entity->getCommentedEntity(); $build[$id]['#entity'] = $entity; + $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle(); + - $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . ($commented_entity ? $commented_entity->bundle() : ''); $display = $displays[$entity->bundle()]; if ($display->getComponent('links')) { $build[$id]['links'] = [ @@ -165,8 +164,7 @@ $build[$id]['#attached'] = []; } $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer'; + if ($attach_history && $commented_entity->getEntityTypeId() === 'node') { - if ($attach_history && $commented_entity - && $commented_entity->getEntityTypeId() === 'node') { $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator'; // Embed the metadata for the comment "new" indicators on this node. reverted: --- b/core/modules/comment/src/Entity/Comment.php +++ a/core/modules/comment/src/Entity/Comment.php @@ -203,8 +203,8 @@ */ public function referencedEntities() { $referenced_entities = parent::referencedEntities(); + if ($this->getCommentedEntityId()) { + $referenced_entities[] = $this->getCommentedEntity(); - if ($commented_entity = $this->getCommentedEntity()) { - $referenced_entities[] = $commented_entity; } return $referenced_entities; }