Is it possible to add a visibility restriction to article comments? (For instance, only the poster can view comments.)


A way by which you can elegantly do this from a custom module would be:

 * Implements hook_node_view_alter().
 function my_mod_node_view_alter(&$build) {
   $node = $build['#node'];
   if (isset($build['comments'])) {
     // It is good practice to allow the administrator to see comments of all nodes.
     $build['comments']['#access'] = 
       user_access('administer comments') || ($node->uid == $GLOBALS['user']->uid);
  • this code hides the comments which are not made by the poster of any posts, but it doesnt show the comments made on his/her own posts.. any way to alter the codes? – chuying Dec 28 '12 at 9:49
  • Fixed a small typo in the reference to the PHP $GLOBALS variable. Can try again now? – Amarnath Ravikumar Dec 28 '12 at 12:14

I can't find contrib module for your task. I suggest you to write some code. In template.php add template_preprocess_node()

function THEMENAME_preprocess_node(&$variables){
  global $user;
  $variables['some_var'] = ($user->uid == $variables['node']->uid) ? TRUE : FALSE;

Than in node.tpl.php

<?php if ($some_var): ?> <?php print render($content['comments']); ?> <?php else: ?> <?php print 'You have no access to see comments'; ?> <?php endif; ?>

Please tell me, if be some problems.

  • Even though your approach should work to hide comments, print statement will print the text at the very top of the browser because output has not started yet (we are PREprocessing right ?) – AyeshK Dec 21 '12 at 17:18

If you are using panels. In the content visability section you can set that up.

  • Can you expand a little on how this is done? – mpdonadio Dec 28 '12 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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