4

I have created two books, and both of them have child pages (which are also book pages). I have activated the option of commenting, in which the comment box is shown in the same book page.

All works fine when there is no comment on a particular page, but even when any user posts at least one comment on the page, the "Add New Comment" button appears in the links area below page-links and above the comment-wrapper; the comment box is shown below. I just wanted to ask how to remove this link because it is messing with the other options in the links area (e.g. printer friendly page, PDF).

I did create a hook using the following code.

<?php
  /**
  * Implementation of hook_link_alter
  * Remove add comment link from a content type
  * MODULENAME = remove_comment
  **/
  function MODULENAME_link_alter(&$links, $node){
    if($node->type == 'book'){
      unset($links['comment_add']);
    }
  }
?>

I have set the permissions for the users to post comments and have kept the comment box open by default. So, it makes no sense in having the Add new comment link.

And in admin/structure/types/manage/book, the settings are as shown below.

snapshot

0

4 Answers 4

5

I did get the solution and was able to remove the "Add new comment" link, from the links area. I just went to comment module and commented the following lines of code on line #675.

// Show the "post comment" link if the form is on another page, or
// if there are existing comments that the link will skip past.
if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($node->comment_count) && user_access('access comments'))) {
  $links['comment-add'] = array(
    'title' => t('Add new comment'),
    'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')),
    'href' => "node/$node->nid",
    'fragment' => 'comment-form',
  );
  if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) {
    $links['comment-add']['href'] = "comment/reply/$node->nid";
  }
}

I know it is not recommended to modify the core. I have implemented a hook to remove the link.

Solution found
The following hook in my custom module works for me :)

function MODULENAME_node_view_alter(&$build) {
  if ($build['#node']->type == 'book') {
    unset($build['links']['comment']['#links']['comment-add']);
  }
}
2
  • 1
    Yes, hacking core is not a good idea. I can see you've amended your OP with the code I suggested. Did you change MODULENAME to something different, something meaningful? This is recommended. Is the new module activated?
    – md2
    May 7, 2012 at 8:51
  • @md2 Yes i did give a name to it as remove_comment, and also enabled it. I also checked with my cache cleared..
    – AjitS
    May 7, 2012 at 8:55
4

You can edit the comment settings per content type or node to close comments which will remove the add new comment link.

Or alternatively you could do this programatically with a snippit of code added to your template.php

function THEMENAME_preprocess_node(&$variables) {
//find the comment link
print_r($variables);

  if ($variables['node']->nid = #) {
    unset($variables[*find the comment link*]);
  }
}

Replace the # with the book page nid you want to effect, the THEMENAME with your themes name and also find the comment link with the array key which contains the add comment link

2
  • 1
    I would rather change the comment settings for the content type. Using code should be done if there are cases where the link should appear, and cases where the link should not appear.
    – apaderno
    Mar 28, 2012 at 11:50
  • I don't want to close the comments, I just want the comment box to be open always and to remove the link.
    – AjitS
    May 4, 2012 at 5:21
2

You could do it using hook_link_alter, an example below. This needs placed into a custom module and not template.php.

<?php
/**
* Implementation of hook_link_alter
* Remove add comment link from a content type
**/
function MODULENAME_link_alter(&$links, $node){
  if($node->type == 'YOURNODETYPE'){
    unset($links['comment_add']);
  }
}
?>
6
  • I am a newbie in Drupal. I didn't know how to implement hooks, I did learn it and created my custom module and implemented the hook above. But, the problem remains. I used $node type = 'book' in it, did I do it write?
    – AjitS
    May 3, 2012 at 14:09
  • @Ajit Could you paste your function so we can rule that out. 'book' is the default machine name for the Book content type. Are you using the Book functionality on a custom content type?
    – md2
    May 3, 2012 at 16:52
  • @Ajit Having just glanced at your comment again, it looks like you are missing an '=' sign in your check for content type. '=' will assign a value '==' will check what a value is equal to. See Comparison Operators.Hope this helps.
    – md2
    May 3, 2012 at 16:58
  • :yes id did use == but missed to mention it above. And my code is shown on the edit above.
    – AjitS
    May 4, 2012 at 5:08
  • Please look for my answer.
    – AjitS
    May 4, 2012 at 12:37
1
// $(this).html('<a href="' + action + '" id="comment-form-title">' + title + '</a>');

Comment the above line in the ajax_comments.js file which is present in AJAX Comments module.

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