views:

31

answers:

2

This is a strange one and not ideal, but basically we have a Drupal site that has been using the core comments module to allow authenticated users to post and view comments for a node. We are now needing to extend this so that unauthenticated users can also post comments, but not view them. Authenticated users would only have access to view comments by other authenticated users. Only admin users would have access to view all comments.

Is there a way of doing this? The permissions do not seem to permit this flexibility (access comments is required to give a user the form, but shows all approved comments).

I've looked into using some Drupal hooks, but as far as I could see there is only the hook_comment function that occurs after the comments have been retrieved (perhaps there is a good way of making use of this, but my mind has gone blank).

Any suggestions? I appreciate hiding comments from certain users but allowing them to post them is probably not best practice; however, I need to work with what we've already got.

A: 

Can't you use the comment moderation queue to achieve exactly this? Check your settings for the comment module (at admin/content/comment).

You can set in your theme template.php whether you want anonymous users to view comments simply by doing a check on whether a user is logged in, and not showing the comment if they're not.

eg:

function mytheme_preprocess_comment(&$vars, $hook) {
  global $user;
  if (!$user->uid) {
    unset($vars);
  }
}

Then in your comment.tpl.php, at the top:

if ( isset($content) ) :

To check whether the comment has been unset, so that you don't render a whole lot of empty divs.

There's undoubtedly better ways to do this which involve using a hook to avoid loading up the comments at all for anon users, but this code will do the job without much work.

cam8001
Unless I'm missing something, this will not keep authenticated users from seeing comments made by the anonymous user.
googletorp
You would control them by using the moderation queue. Admin users would see all, auth users would see only approved, and anonymous users would see none. You set authenticated user's to have the 'post comments without approval' permission.
cam8001
+1  A: 

I can think of a way to solve this problem, but it's not the prettiest solution.

  1. Create a custom module with your own permissions.
  2. Overwrite the standard theme function that's used to render the comments. You should have the comment object available, so it should be fairly easy to see if the comments is from the anonymous user or a registered user, and can check which comments the user should be able to see.

Now there's a few problems in this, as comments can be threaded, so in theory, it is possible that a comment in a thread will be missing causing confusion and possible other bugs.

But like I said, this is a quick and dirty fix, with what you got.

googletorp
Thanks for the response, I'm going with the custom module approach and using hook_db_rewrite_sql() (which I keep forgetting about) to add some additional conditions to the comment query. I want to try and keep it all in a module and out of the templates as we are probably going to be re-theming the site at a later phase. I shouldn't have an issue with threaded comments, but it is a good point that I'll bear in mind.
monkeyninja