0

The comments on my articles have a Fivestar field called 'Stars', but rating nodes isn't always required (see: How to hide rating field when adding comment to own node?). When the user didn't rate the node on which he/she is reacting, 0 stars (not saved in the database) are now being displayed in his/her comment, but I don't really like that.

Comment without rating

How can I hide the stars when the user didn't vote?


Edit: I'm trying to find a workaround with flags. I've created a flag called 'Reaction on own' and a rule that flags a comment when the comment's author equals the author of the commented node. In my case, it's sufficient that the 'Stars' field isn't displayed when a comment is flagged with 'Reaction on own'. This is the code of my custom module:

function hiderating_comment_view($comment) {
  $flag = flag_get_flag('reaction_on_own');
  if ($flag->is_flagged($comment->cid)){
    unset($comment->field_stars);
  }
}

Unfortunately, this isn't working. What's going wrong?


Note: In fact, the edit above isn't entirely true in my case, because 'zero' stars appear when a user reacts on his/her article or when the user has commented before. I've provided an answer for both cases.

  • Please check if the 'no value' value is stored in a different way from the '0 star' value so I can help you ^^ – Shyghar Nov 20 '13 at 17:05
  • @Shyghar: When users leave a comment without voting, there's no '0' value saved to the database, if that's what you mean. – Jeroen Nov 20 '13 at 17:12
  • if ($comment == "comment_node_article") { is incorrect has hook_comment_view is passed the comment object, not a string in that parameter. It will never evaluate to true. – David Thomas Nov 26 '13 at 21:24
  • @David Thomas: Thanks for your reaction. I don't really understand what you wrote there, but I could deduct that my code isn't working. Do you have any suggestions for improvement? I'm new to PHP, so I don't know a lot about using hooks. I've just tried something, because nobody seems to know a solution for a over week now. – Jeroen Nov 27 '13 at 21:26
  • @Jeroen tentative solution added – David Thomas Nov 27 '13 at 22:03
0

You could try an approach like this (untested) but may work

/**
 * Implements hook_comment_view
 */
function hiderating_comment_view($comment) {
  if($flag = flag_get_flag('reaction_on_own')){
    // Debug comment to find field to hide, check dblog / watchdog
    debug($comment);
    if (!$flag->is_flagged($comment->cid)){
      unset($comment->field_stars);
    }
  }
}
  • I tested it, but, although it doesn't return any errors, the 'empty' stars are still there after clearing caches and adding new comments. – Jeroen Nov 27 '13 at 22:21
  • OK, you'll need to dump out the comment to find where the relevant field to unset is. Answer updated with debug info – David Thomas Nov 27 '13 at 22:41
  • Hmm, odd that should be fine there. You can try debug() instead and check the watchdog log in admin. Otherwise need another way to determine flag info on comment. – David Thomas Nov 27 '13 at 23:58
  • I thought the error we discussed earlier was possibly a result of a corrupt database, but even with a fresh installation I get the error "Fatal error: Nesting level too deep - recursive dependency? in C:\wamp\www\mysite\includes\common.inc on line 7517" when I add the debug line. Do you know what causes this? I'd really like the code to work... – Jeroen Feb 1 '14 at 0:32
0

I'm excited to announce that I finally found a working solution! It turned out to be a bit more difficult than I had assumed in the beginning: I had to work with flags, rules and a custom module. Perhaps it was also possible to achieve this without flags, but they can come in handy elsewhere on one's website (e.g. for granting userpoints to users), so that's a little bonus.

Step 1: Create a flag for comments 'First reaction on article'.

Step 2: Create a flag for articles 'Commented on this article'.

Step 3: Create a rule similar the one with the following settings:

{ "rules_new_reaction_on_article" : {
    "LABEL" : "New reaction on article",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "flag", "comment" ],
    "ON" : { "comment_insert--comment_node_article" : { "bundle" : "comment_node_article" } },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "comment:author" ],
          "roles" : { "value" : { "2" : "2" } },
          "operation" : "OR"
        }
      },
      { "NOT flag_flagged_node" : {
          "flag" : "commented-on-this-article",
          "node" : [ "comment:node" ],
          "flagging_user" : [ "comment:author" ]
        }
      },
      { "NOT data_is" : { "data" : [ "comment:author" ], "value" : [ "comment:node:author" ] } }
    ],
    "DO" : [
      { "flag_flagnode" : {
          "flag" : "commented-on-this-article",
          "node" : [ "comment:node" ],
          "flagging_user" : [ "comment:author" ],
          "permission_check" : 1
        }
      },
      { "flag_flagcomment" : {
          "flag" : "first-reaction-on-article",
          "comment" : [ "comment" ],
          "flagging_user" : [ "comment:author" ],
          "permission_check" : 1
        }
      }
    ]
  }
}

Step 4: Create a custom module 'Hiderating':

function hiderating_comment_view($comment) {
  $flag = flag_get_flag('first-reaction-on-article');
  $uid = db_query('SELECT n.uid FROM {node} n INNER JOIN {comment} c ON c.nid = n.nid WHERE c.cid = :cid', array(':cid' => $comment->cid))->fetchField();
  if ($comment->uid == $uid OR !$flag->is_flagged($comment->cid, $comment->uid)){
    $comment->content['field_stars']['#access'] = FALSE;
  }
}

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.