diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index 93040333eb..4701847ce7 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -198,13 +198,11 @@ function comment_tokens($type, $tokens, array $data, array $options, BubbleableM case 'parent': if ($comment->hasParentComment()) { + $replacements[$original] = '[not found]'; if ($parent = $comment->getParentComment()) { $bubbleable_metadata->addCacheableDependency($parent); $replacements[$original] = $parent->getSubject(); } - else { - $replacements[$original] = '[not found]'; - } } break; diff --git a/core/modules/comment/src/CommentLazyBuilders.php b/core/modules/comment/src/CommentLazyBuilders.php index e2b1666402..3eaf7065bd 100644 --- a/core/modules/comment/src/CommentLazyBuilders.php +++ b/core/modules/comment/src/CommentLazyBuilders.php @@ -139,7 +139,7 @@ public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_pr $entity = $this->entityTypeManager->getStorage('comment')->load($comment_entity_id); $commented_entity = $entity->getCommentedEntity(); - $links['comment'] = $commented_entity ? $this->buildLinks($entity, $commented_entity) : []; + $links['comment'] = $this->buildLinks($entity, $commented_entity); // Allow other modules to alter the comment links. $hook_context = [ @@ -165,6 +165,12 @@ public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_pr */ protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) { $links = []; + + // No links to return if this is an orphan. + if (!$commented_entity) { + return $links; + } + $status = $commented_entity->get($entity->getFieldName())->status; if ($status == CommentItemInterface::OPEN) { diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index 1e85361f1d..1a69a72532 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -144,9 +144,7 @@ public function buildComponents(array &$build, array $entities, array $displays, $commented_entity = $entity->getCommentedEntity(); $build[$id]['#entity'] = $entity; - $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' - . ($commented_entity ? $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'] = [ @@ -168,7 +166,7 @@ public function buildComponents(array &$build, array $entities, array $displays, } $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer'; if ($attach_history && $commented_entity - && $commented_entity->getEntityTypeId() === 'node') { + && $commented_entity->getEntityTypeId() === 'node') { $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator'; // Embed the metadata for the comment "new" indicators on this node. diff --git a/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php b/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php index 7d61672fb9..03c09696a7 100644 --- a/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php +++ b/core/modules/comment/tests/modules/comment_test/src/CommentTestSqlEntityStorage.php @@ -1,10 +1,5 @@ cid_max)) { - $comment_ids = array($row->cid_max); + $comment_ids = [$row->cid_max]; } if (!empty($comment_ids)) { - $update_values = array('entity_id' => $row->entity_id_max + 1, 'pid' => $row->pid_max + 1, 'uid' => $row->uid_max + 1); + $update_values = [ + 'entity_id' => $row->entity_id_max + 1, + 'pid' => $row->pid_max + 1, + 'uid' => $row->uid_max + 1, + ]; if ($fields) { $update_values = array_intersect_key($update_values, array_flip($fields)); } diff --git a/core/modules/comment/tests/src/Functional/CommentOrphanedTest.php b/core/modules/comment/tests/src/Functional/CommentOrphanedTest.php index 920703d2e4..de56a8c24e 100644 --- a/core/modules/comment/tests/src/Functional/CommentOrphanedTest.php +++ b/core/modules/comment/tests/src/Functional/CommentOrphanedTest.php @@ -1,16 +1,12 @@ getStorage('comment'); if (in_array('Drupal\Core\Entity\Sql\SqlEntityStorageInterface', class_implements($storage))) { + $session = $this->assertSession(); // Make sure we have a comment. $this->drupalLogin($this->webUser); @@ -60,7 +55,12 @@ public function testDeleteOrphanedComment() { $subject = $this->randomMachineName(); $this->postComment($this->node, $comment_text, $subject); - // Mess with the comment to make it orphaned. + $this->drupalGet('node/2'); + $this->drupalGet('node/' . $this->node->id()); + $session->pageTextContains($comment_text); + $session->pageTextContains($subject); + + // Make the comment an orphan. $original_class = get_class($storage); $manager->clearCachedDefinitions(); $manager->getDefinition('comment') @@ -71,10 +71,10 @@ public function testDeleteOrphanedComment() { $storage->orphanComments([], ['pid', 'uid']); // Render the entity, with orphaned comment, implicitly testing that - // nothing breaks. (setUp() configured the field so that in 'full' i.e. - // default view mode, comments are rendered.) - $built = $this->drupalBuildEntityView($this->node); - \Drupal::service('renderer')->renderPlain($built); + // nothing breaks. + $this->drupalGet('node/' . $this->node->id()); + $session->pageTextContains($comment_text); + $session->pageTextContains($subject); $ids = $storage->orphanComments(); @@ -84,12 +84,13 @@ public function testDeleteOrphanedComment() { // are no standalone 'comment pages') but it ensures some decoupling of // the comment view/render code, which is a good thing in itself. $entities = $storage->loadMultiple($ids); - $built = $this->drupalBuildEntityView(reset($entities), 'full', NULL, TRUE); - \Drupal::service('renderer')->renderPlain($built); $storage->delete($entities); - $manager->clearCachedDefinitions(); $manager->getDefinition('comment')->setStorageClass($original_class); + + $this->drupalGet('node/' . $this->node->id()); + $session->pageTextNotContains($comment_text); + $session->pageTextNotContains($subject); } }