3

On my site we have enquiries (nodes) and price quotes (comments). So node authors shouldn't be offered to quote prices to themselves.

That's why I want to disallow node authors comment their own nodes. How can I do so?

Updated: The 'user-compare' option in panels visibility rules could easily allow to achieve what i want. I've used this functionality for other panes i.e. profile fields etc. But i didn't find 'user-compare' entry in Panels visibility rules for comment form in node template. Why?

I chose the way offered by Pierre.Vriens using Rules because I couldn't make the code snippet by oksana-c work with Panels. Unpublishing comments with The Rules is not best user experience. Comment Permissions module also didn't help. Any suggestion for unsetting whole comment form for node authors programmatically in Panels environment would be much appreciated.

8
  • 1
    are you using D7 or D8?
    – oksana-c
    Feb 15, 2016 at 10:27
  • 1
    what about replies to comments? author of the node should be disallowed to comment AND reply?
    – oksana-c
    Feb 15, 2016 at 10:33
  • Thnx oksana, I am using D7. Neither authors nor other users will reply to comments in their own or other's nodes. Users only can comment in other's nodes and no replies are needed on these comments. Feb 15, 2016 at 11:20
  • 1
    how are you using Panels? are you overriding node/%node path with Panels? or maybe you're displaying multiple nodes on one Panel page w/ comments under each node? Are you adding comment block separately from the node? Unfortunately, Rules are not the solution here. Unpublishing comment after it has been posted is not an ideal user experience. Look into extending Comment Permissions module. That is your best bet.
    – oksana-c
    Jun 4, 2016 at 16:40
  • 1
    Of course, Comment Permissions will be useless for your implementation if you take it and use it as it comes out of the box. That is why I suggested "extending" it, which is not difficult, especially with your requirements.
    – oksana-c
    Jun 15, 2016 at 0:01

3 Answers 3

2

You can use the Rules module for this, by creating a rule that looks like so:

  • Rules Event: After saving a new comment.

  • Rules Condition: Data comparison like comment:author is equal to comment:node:author.

  • Rules Action: Set a data value for comment:status, and set it to unplublished (which you should be able to do if using the Rules event "After saving a new comment").

Here is the actual Rule I created for it (in Rules export format, which you should be able to import in your own site aso, using the Rules UI):

{ "rules_unpublish_selected_comments" : {
    "LABEL" : "Unpublish selected comments",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "comment" ],
    "ON" : { "comment_insert" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "comment:node:author" ], "value" : [ "comment:author" ] } }
    ],
    "DO" : [ { "data_set" : { "data" : [ "comment:status" ], "value" : "0" } } ]
  }
}

After you imported this rule, you must first "enable" it before it actually becomes active i your own site (alternative: exclude the line with "ACTIVE" : false, before importing this rule.

Consider the above as a "template Rule", which with some minor tuning you can also use for all sorts of variations of processing comments via the Rules module ... (eg by using some other Rules Condition).

If you're not familiar with the Rules module, have a look at the video about Rules(Sending Emails) - Comment reply notification which demonstrates how to use the Rule UI to build a rule that is about a variation of this question.

Happy Rules-ing!

PS: No need for custom PHP coding ...

0
3

You'd want to use hook_node_view_alter() in your theme to unset the comment form if the logged in user is same as node author.

function yourtheme_node_view_alter(&$build) {
    global $user;
    // get the uid of logged in user
    $logged_in_uid = $user->uid;

    // if content type is 'your_content_type'
    if ($build['#bundle'] == 'your_content_type') {

        //get node author's uid
        $node_author = $build['#node']->uid;

        // if logged in user is the node author - unset comment form and remove the 'Add comment' links from node display
        if ($node_author == $logged_in_uid) {
            unset($build['comments']['comment_form']);
            unset($build['links']['comment']['#links']['comment-add']);
        }
    }
}

Also, you can check out Comment Permissions module. It does not offer the functionality that you need, but it provides more granular permissions than core comment module.

The Comment Permissions module enables control of commenting by user role and by node type. Additional user permissions for selected node types are added to the user access system so you can configure commenting with more control than Drupal core provides.

3
  • Nice, the hook_node_view_alter() worked just fine Feb 16, 2016 at 6:53
  • I added a message to yr code to check if function works. The message is displayed on front page, not on node/view page. Jun 13, 2016 at 17:32
  • @TimurKutuev hook_node_view_alter() will run on every page where function node_view() runs. This means the message that you added will be displayed on every page where you display any nodes in any view mode. Information that you provided is not enough to troubleshoot.
    – oksana-c
    Jun 14, 2016 at 23:58
0
  • Log into the Drupal Dashboard
  • Click on Structure from the upper menu bar and then select the Content types category.
  • Click Edit next to the name of the content type you wish to edit
  • drupal_global_comment_1
  • Scroll down and click on Comment settings
  • drupal_global_comment_2
  • Select Closed from the drop down menu under Default comment setting for new contentdrupal_global_comment_3
  • Click the Save content type button at the bottom of the page.
1
  • Thnx Raj, but if I do so, i would completely kill commenting in the nodes. I only need to disallow node author to comment on their own nodes but let other users be able to comment. I saw several issues of disallowng authors to rate their own nodes and this is the way it should be done with the comments in my case. Feb 15, 2016 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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