Search code examples
drupaldrupal-6drupal-comments

Controlling which comments are shown based on role in Drupal


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.


Solution

  • 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.