4

On my website, users can rate nodes with the Fivestar module while adding a comment. In other words, the rating field (called 'stars') is part of the comment. Now, I'd like to hide the rating field for users when they comment on their own nodes (or when they already have commented before - see: How to hide Fivestar rating field after first comment?).

How can I do that?


Edit 1: I applied this patch (patch 97 on https://drupal.org/node/189527), but my problem still isn't solved. I now have the following option at Administration > Structure > Content types > MyArticle > Comment fields:

Vote on own comment

But after clearing caches and creating new content, the rating field is still displayed when I add a new comment.


Edit 2: I sent an e-mail to the patch's author and he answered this:

The purpose of the patch I created is to prevent a user from rating their own node. I believe the problem you are having with this patch is that, since the fivestar field is on the comment, that the patch I wrote would probably reference the comment itself (as the entity id) instead of the node - freely allowing someone to vote.


Edit 3: I tried to solve my problem with a custom module called 'hiderating'. This is the code I'm using:

function hiderating_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == "comment_node_stuff_form") {
   if ($form['#node']->uid == $user->uid) {
      $form['field_stars']['#access'] = 0;
      }
      else {
      $form['field_stars']['#required'] = 1;
      }
    }
}

I made the 'Stars' field not required from the interface. The code is nearly working: the 'Stars' field isn't displayed when one comments on his/her own node, but unfortunately, it isn't turned to 'Required' when one comments on someone else's node. In other words, my code's else part doesn't work properly.

How can this be fixed?

7
  • Did you check the issue queue, we have a issue for this. drupal.org/node/189527. Patch 97 was the fix for that.
    – Gladiator
    Oct 30, 2013 at 5:27
  • @Gladiator: I applied the patch you mentioned, but it doesn't work (see edit 1). The patch's author Travis thought that it's possibly not suitable for my problem (see edit 2).
    – Jeroen
    Oct 30, 2013 at 17:41
  • why do you need an else part? Nov 18, 2013 at 14:27
  • @Mohammed Shameem: well, I'd like the rating field to be required, but when I mark it as 'required' via the interface, people adding a comment to their own node get an error, because they didn't rate it (the field was hidden).
    – Jeroen
    Nov 18, 2013 at 15:37
  • 1
    have you tried using unset($form['field_stars']); instead of $form['field_stars']['#access'] = 0; and also make the field as required and remove the else part Nov 18, 2013 at 15:58

1 Answer 1

1

You should use unset($form['field_stars']); instead of $form['field_stars']['#access'] = 0;

You can keep the fields as required and also remove the else condition.

So your final code would be something like this

function hiderating_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == "comment_node_stuff_form") {
   if ($form['#node']->uid == $user->uid) {
      unset($form['field_stars']);
      }
    }
}

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.