开发者

Controlling which comments are shown based on role in Drupal

开发者 https://www.devze.com 2023-01-05 17:39 出处:网络
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 needi

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 i开发者_如何学Pythons probably not best practice; however, I need to work with what we've already got.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号