2

Currently, I have a list of teasers and they have a comment count and an 'Add new comment' link.

That link goes to a dedicated comment page with a URL of the form "/comment/reply/19#comment-form".

However, I want it to go to the actual node which has the comment form at the bottom (eg. "/node/19#comment-form").

Maybe this can be done is the template.php or some other way. Any ideas would be greatly appreciated.

1 Answer 1

1

Go to the admin page for your content type

/admin/structure/types/manage/page

and check Show reply form on the same page as comments under Comment settings.

If you look at comment_node_view() in the comment module, you can see where the comment link is added to teasers.

    if (user_access('post comments')) {
      $links['comment-add'] = array(
        'title' => t('Add new comment'),
        'href' => "comment/reply/$node->nid",
        'attributes' => array('title' => t('Add a new comment to this page.')),
        'fragment' => 'comment-form',
      );
    }

To alter the link you can use hook_node_view_alter() in your module.

function MODULE_node_view_alter(&$build) {
  $node = $build['#node'];
  if (isset($build['links']['comment']['#links']['comment-add'])) {
    // Alter the 'Add new comment' link.
    $build['links']['comment']['#links']['comment-add']['href'] = "node/$node->nid";
    // Fragment is the '#comment-form' part of the url.
    $build['links']['comment']['#links']['comment-add']['fragment'] = 'comment-form';
  }
}
7
  • Thanks for the reply but I've already got the comment form showing on the node page. I want the 'Add New Comment' link on the teaser to go to that node page (and not the dedicated Comment page it currently does go to).
    – Podsnap
    Jan 26, 2015 at 15:38
  • I added a description how to alter the Add new comment link. Jan 27, 2015 at 10:46
  • Thank you so much Marcus. This did exactly what I wanted. I had to put single quotes around the #node on the second line, so the line was: $node = $build['#node']; but otherwise it worked wonderfully, thank you.
    – Podsnap
    Jan 29, 2015 at 10:12
  • @MarcusAschan this works great, but would you happen to know how to add HTML to the 'title' (that on default renders 'Add new comment')? I'm trying to add a FontAwesome icon (<i class="fa fa-comments" aria-hidden="true"></i>) before this and it's currently showing up as plain text.
    – WebMW
    Sep 28, 2017 at 18:26
  • 1
    @WebMW you should be able to render HTML by adding $build['links']['comment']['#links']['comment-add']['html'] = TRUE;. You can check the documentation for theme_links for more options. Oct 4, 2017 at 7:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.