2

In Drupal there is a permission "view comments" and "add comments". I would like to activate "add comments" and disactivate "view comments". How to allow to view your own comments for users? It's a little bit strange to publish comments and they disappear into nowhere.

3

I think this kind of logic is better handled in a custom module instead of the theme layer. Something like this might work for you:

/**
 * Implements hook_permission().
 */
function mymodule_permission() {
  return array(
    'view own comments only' => array(
      'title' => t('View only own comments'),
      'description' => t('The user will not see comments made by other users.'),
      'warning' => t('Sometimes this might not work...'),
    ),
  );

}

/**
 * Implementation of hook_query_TAG_alter()
 */
function mymodule_query_comment_filter_alter(QueryAlterableInterface $query) {
  if (user_access('view own comments only') && !user_access('administer comments')) {
    $query->condition('c.uid', $GLOBALS['user']->uid);
  }
}

This defines a view own comments only permission. If this is set and the user does not have the administer comments permission, comments made by other users will be hidden on the node page.

There are a few problems with this solution (and you'll probably have the same problems if you are trying to do this in a template file):

  • comment count will not be modified
  • Recent comments block will not be modified
2

I would leave the 'view comments' permission on, and put some code in my comment.tpl.php file to test for ownership of the comment.

The default theme file looks like this:

/** 60 lines of useful information about available parameters */

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php print $picture ?>

  <?php if ($new): ?>
    <span class="new"><?php print $new ?></span>
  <?php endif; ?>

  <?php print render($title_prefix); ?>
  <h3<?php print $title_attributes; ?>><?php print $title ?></h3>
  <?php print render($title_suffix); ?>

  <div class="submitted">
    <?php print $permalink; ?>
    <?php print $submitted; ?>
  </div>

  <div class="content"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['links']);
      print render($content);
    ?>
    <?php if ($signature): ?>
    <div class="user-signature clearfix">
      <?php print $signature ?>
    </div>
    <?php endif; ?>
  </div>

  <?php print render($content['links']) ?>
</div>

Put a copy of the file into your theme's templates directory (and reference it in your theme settings if required). Then alter the file so that the entire section is wrapped in an if statement.

I don't know exactly where the uids of the author and user are stored, you can use the devel module to find that out, simply put <?php dpm($author); ?> in the template file somewhere once you've install devel and you'll see the structure of the $author variable. I have a hunch it might be $author->uid and $user->uid, in which case you can do this:

/** 60 lines of useful information about available parameters */

<?php
 global $user;
 if(author->uid == $user->uid):
?>

// everything from line 61 to 92 in here

<?php endif; ?>
0
  Here I am using content type name (Custom Comments).
  comment.tpl.php file copy code and past it here
  comment--node-custom_comments.tpl.php

   <?php
     global $user;
     if($comment->uid == $user->uid):
   ?>

   // everything from comment.tpl.php code  copy and past here

  <?php endif; ?>

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.