Commit 83054ca9 authored by alexpott's avatar alexpott

Issue #2588547 by mikeryan, dawehner, YesCT: CommentLinkBuilder causes fatal...

Issue #2588547 by mikeryan, dawehner, YesCT: CommentLinkBuilder causes fatal error when comment field not named 'comment'
parent cc3ddd4a
......@@ -208,7 +208,7 @@ public function buildCommentedEntityLinks(FieldableEntityInterface $entity, arra
if ($new_comments > 0) {
$page_number = $this->entityManager
->getStorage('comment')
->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity);
->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments, $entity, $field_name);
$query = $page_number ? ['page' => $page_number] : NULL;
$value = [
'new_comment_count' => (int) $new_comments,
......
......@@ -135,7 +135,7 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div
/**
* {@inheritdoc}
*/
public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name = 'comment') {
public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
$field = $entity->getFieldDefinition($field_name);
$comments_per_page = $field->getSetting('per_page');
......
......@@ -54,7 +54,7 @@ public function getMaxThreadPerThread(CommentInterface $comment);
* @return array|null
* The page number where first new comment appears. (First page returns 0.)
*/
public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name = 'comment');
public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name);
/**
* Gets the display ordinal or page number for a comment.
......
......@@ -335,7 +335,7 @@ public function renderNewCommentsNodeLinks(Request $request) {
$node = $this->entityManager->getStorage('node')->load($nid);
$new = $this->commentManager->getCountNewComments($node);
$page_number = $this->entityManager()->getStorage('comment')
->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node);
->getNewCommentPageNumber($node->{$field_name}->comment_count, $new, $node, $field_name);
$query = $page_number ? array('page' => $page_number) : NULL;
$links[$nid] = array(
'new_comment_count' => (int) $new,
......
......@@ -161,12 +161,28 @@ public function preRender(&$values) {
*/
protected function renderLink($data, ResultRow $values) {
if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
$node_type = $this->getValue($values, 'type');
$node = entity_create('node', array(
'nid' => $this->getValue($values, 'nid'),
'type' => $this->getValue($values, 'type'),
'type' => $node_type,
));
$page_number = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node);
// Because there is no support for selecting a specific comment field to
// reference, we arbitrarily use the first such field name we find.
// @todo Provide a means for selecting the comment field.
// https://www.drupal.org/node/2594201
$entity_manager = \Drupal::entityManager();
$field_map = $entity_manager->getFieldMapByFieldType('comment');
$comment_field_name = 'comment';
foreach ($field_map['node'] as $field_name => $field_data) {
foreach ($field_data['bundles'] as $bundle_name) {
if ($node_type == $bundle_name) {
$comment_field_name = $field_name;
break 2;
}
}
}
$page_number = $entity_manager->getStorage('comment')
->getNewCommentPageNumber($this->getValue($values, 'comment_count'), $this->getValue($values), $node, $comment_field_name);
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $node->urlInfo();
$this->options['alter']['query'] = $page_number ? array('page' => $page_number) : NULL;
......
......@@ -94,6 +94,55 @@ public function testCommentFieldDelete() {
$this->assertEqual(1, count($elements), 'There is one comment field on the node.');
}
/**
* Tests link building with non-default comment field names.
*/
public function testCommentFieldLinksNonDefaultName() {
$this->drupalCreateContentType(['type' => 'test_node_type']);
$this->addDefaultCommentField('node', 'test_node_type', 'comment2');
$web_user2 = $this->drupalCreateUser([
'access comments',
'post comments',
'create article content',
'edit own comments',
'skip comment approval',
'access content',
]);
// Create a sample node.
$node = $this->drupalCreateNode([
'title' => 'Baloney',
'type' => 'test_node_type',
]);
// Go to the node first so that webuser2 see new comments.
$this->drupalLogin($web_user2);
$this->drupalGet($node->urlInfo());
$this->drupalLogout();
// Test that buildCommentedEntityLinks() does not break when the 'comment'
// field does not exist. Requires at least one comment.
$this->drupalLogin($this->webUser);
$this->postComment($node, 'Here is a comment', '', NULL, 'comment2');
$this->drupalLogout();
$this->drupalLogin($web_user2);
// We want to check the attached drupalSettings of
// \Drupal\comment\CommentLinkBuilder::buildCommentedEntityLinks. Therefore
// we need a node listing, let's use views for that.
$this->container->get('module_installer')->install(['views'], TRUE);
// We also need a router rebuild, as the router is lazily rebuild in the
// module installer.
\Drupal::service('router.builder')->rebuild();
$this->drupalGet('node');
$link_info = $this->getDrupalSettings()['comment']['newCommentsLinks']['node']['comment2']['2'];
$this->assertIdentical($link_info['new_comment_count'], 1);
$this->assertIdentical($link_info['first_new_comment_link'], $node->url('canonical', ['fragment' => 'new']));
}
/**
* Tests creating a comment field through the interface.
*/
......
......@@ -250,7 +250,7 @@ function testCommentNewPageIndicator() {
$node = Node::load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned_page = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node);
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
$this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
}
......@@ -269,7 +269,7 @@ function testCommentNewPageIndicator() {
$node = Node::load($node->id());
foreach ($expected_pages as $new_replies => $expected_page) {
$returned_page = \Drupal::entityManager()->getStorage('comment')
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node);
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node, 'comment');
$this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment