Would you mind explaining how I can include the fivestar form in another form? For instance, I've tried to include it in the comment form by doing the following:

function commentrate_comment(&$comment, $op) {

  switch ($op) {

    case 'form':
      $node = node_load($comment['nid']['#value']);

        $form['vote'] = array(
          '#type' => 'fivestar',
          
          '#weight' => 10,
        );
             
      return $form;

  }
}

This results in 6 unthemed radio buttons :-) I guess I need to know what all the other values should be, and if I need to manually configure the path to the css and theme folders for the module.

Thanks,

Doug Gough

CommentFileSizeAuthor
#9 1.png14.34 KBChristianVK
#9 2.png8.89 KBChristianVK

Comments

Politosphere’s picture

Hi,

Sorry I don't have a solution to your problem, but I'm interested, because I was wondering the same thing: it would be nice to rate comments also.

Did you figure out how to do it on your own, or are you still looking for assistance?

quicksketch’s picture

Component: Documentation » Code
Category: support » feature

You're close :)

A fivestar type form element requires some additional support, as without javascript or css it is, as you said, just radio buttons. Try:

        // Add css
        drupal_add_css(drupal_get_path('module', 'fivestar') .'/theme/fivestar.css');
        // Add js
        drupal_add_js(drupal_get_path('module', 'fivestar') . '/jquery.rating.js');
        drupal_add_js("jQuery(function(){jQuery('div.fivestar-widget').rating();});", 'inline');

        $form['vote'] = array(
          '#type' => 'fivestar',
          '#weight' => 10,
        );

Just typing this makes me realize that Fivestar needs to provide some kind of utility to do this code for you (or do it automatically). I'm marking this as a feature request :)

Anonymous’s picture

I ended up coding a custom module that did this and a few other things. My problem was that I needed to allow people to add a rating of the original article when they submitted a comment on that article, and I needed to have some validation that would not allow them to submit a comment without a rating. As a side note, this turned out to be a fascinating journey into the guts of Drupal's API, and I'm glad I took the trip. I feel like a real Drupal developer now :-) I'm happy to share the code with anyone who wants it. I doubt that it will ever find it's way into the contributed modules.

lzy’s picture

duggoff, I will really appreciate it if you could share the code. Been looking for the answer for quite some time already. :)

Leeteq’s picture

Second that. interesting feature.
Do you mind sharing the module?

Anonymous’s picture

Here's the solution I came up with. I'm learning more every day about the forms API so I'm not sure if this is really the right way to do it.

You need to create a module with this code in it:

/**
 * Implementation of hook_comment().
 */
function commentrate_comment($a1, $op) {
  global $node;
  switch ($op) {

    case 'form':
       $currentnode = $a1[nid]['#value'];
       $form['ratings'] = array(
         '#type' => 'fieldset',
         '#collapsible' => FALSE,
         '#prefix' => '<div class="fivestar-fieldset">',
         '#suffix' => '</div>',
         '#weight' => 0,
         );
       $form['ratings']['fivestar_caption'] = array(
         '#value' => t('How do you feel about this article?'),
         '#prefix' => '<div class="fivestar-comment">',
         '#suffix' => '</div>',
         );
       $form['ratings']['fivestar_widget'] = array(
         '#value' => fivestar_widget_form($currentnode),
         );
       return $form; 
    break;

  }
} 

And you need to make a modification to the 5 Star module:

// Changed the function so that it accepts the node id instead of the node array as an argument. Now I can use it in the forms section of hook comment.

function fivestar_widget_form($nid) {
  if (strpos(drupal_get_js(), 'jquery.rating.js') === FALSE) {
    drupal_add_js(drupal_get_path('module', 'fivestar') . '/jquery.rating.js');
    drupal_add_js("jQuery(function(){jQuery('div.fivestar-widget').rating();});", 'inline');
  }
  if (strpos(drupal_get_js(), "jQuery('input.fivestar-submit').hide()") === FALSE) {
    drupal_add_js("jQuery(function(){jQuery('input.fivestar-submit').hide();});", 'inline');
  }
  return drupal_get_form('fivestar_form_node_' . $nid, 'node', $nid);
}

quicksketch’s picture

Status: Active » Fixed

I've moved the javascript and CSS calls around so adding a fivestar element is much easier:

$form = array(
    '#type' => 'fivestar',
    '#stars' => 5,
);
print drupal_render($form);

Take a look at the fivestar_form() function for more implementation details.

Anonymous’s picture

Status: Fixed » Closed (fixed)
ChristianVK’s picture

Version: 5.x-1.3 » 6.x-1.19
Category: feature » support
StatusFileSize
new8.89 KB
new14.34 KB

Hi there,

i hope i am on the right place here to ask.

Ive got the following problem:

My users are writing reviews (node-type = review) for products. They write a text about the quality of the product and then should be able to rate the product by fivestars on the same form.
This review should only be rateable by the user who wrote it (the author) and nobody else.

Others Users can then read the review of the Author and see the fivestar-rating beneath the review, but cant manipulate this vote.

So i have the following steps to accomplish:
Step 1:
Show the fivestar-rating inside the form, when creating a new node (of type "review")

Step 2:
Statically show the rating under the review for other users to see on the website.

Here are my solutions so far:

Step 1: Ok! I inserted the code from above into an own module with the hook_form_alter

function mymodule_form_alter(&$form, $form_state, $form_id)
{
    if($form_id == 'review_node_form')
    {
        // Add css
        drupal_add_css(drupal_get_path('module', 'fivestar') .'/theme/fivestar.css');
        // Add js
        drupal_add_js(drupal_get_path('module', 'fivestar') . '/jquery.rating.js');
        drupal_add_js("jQuery(function(){jQuery('div.fivestar-widget').rating();});", 'inline');

        $form['vote'] = array(
          '#type' => 'fivestar',
          '#weight' => 10,
        );
    }
}

Done! The fivestar-rating will show up on creating a new node of the type "review".
I fill in my review text and then I click on the fivestar-rating to vote inside the form and then submit the form.

Heres a screenshot:

Step 2: not ok
The created review looks fine in the public view, but the Stars are still set to nothing (like i never voted)

Screenshot:

Does somebody have a hint for me, how i can make this rating possible?
I think the problem is, that im only showing the fivestar-rating on node-creation but it runs no functionality when i submit the form to create the review.