6

Is it possible to change the "add new comment" text on certain content types? Also is it possible to only allow certain roles to post comments on this content type?

4 Answers 4

5

Check the roles/permission grid to restrict certain roles posting comments.

hook_node_view_alter(&$build) would help to replace the label with your custom one.

e.g.

 function MODULE_node_view_alter(&$build){
    if ( in_array($build['#node']->type, array('page')) )
    $build['links']['comment']['#links']['comment-add']['title'] = l('New Label', 'comment/reply/'. $build['#node']->nid);

There can be another easy way using String Overrides but it will not let you do on specific content types.

1

Node.tpl.php has this "$content['links']['comment']['#links']['comment-add']" in the code. Maybe take closer look at that array with dpm (devel module) or just print_r, to see if you can alter this or just alter the final markup for that link. Here you have list of variables you can use when you override node.tpl.php: http://api.drupal.org/api/drupal/modules!node!node.tpl.php/7

You can use one node.tpl.php or keep different node.tpl.php files for different content types - check out the naming convention (something like node--content-type.tpl.php)

1

You can easily do this by editing the comment-wrapper.tpl template just copy this template from the core comment module and paste it in your theme. You will find the below code in the template. For roles based comment you can add logic to your node template.


print t('Add new comment');

2
  • It's not such a good idea to edit core though, you might lose the changes when Drupal is updated.
    – tomhallam
    Nov 16, 2012 at 11:47
  • 1
    @tomhallam Mohammad Anwar's answer is not editing the core. Editing a copy of file comment-wrapper.tpl in your own theme is not editing the core because the edited code resides outside of the core, in your own theme. Overriding in this way is common practice in themeing. Jul 3, 2013 at 14:27
0

For the label part, possibly simplest to override the template in your theme.

In Drupal 8, it uses field--comment.html

You can name it by content type, e.g.

field--node--field-comments--foo.html.twig

Enable Twig debugging to get all the possible filenames.

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