7

How do you add a FiveStar widget to a (non-node) form using hook_form_alter?

2 Answers 2

5
  $path = drupal_get_path('module', 'fivestar');      
  drupal_add_js($path . '/js/fivestar.js');
  drupal_add_css($path . '/css/fivestar.css');

  $rating = votingapi_select_single_result_value(array(
        'entity_id' => YOURENTITY_ID,
        'entity_type' => 'YOURENTITY_TYPE',
        'tag' => 'vote',
        'function' => 'average',
   ));
   $form['votes'] = array(
      '#markup' =>  theme('fivestar_static', array('rating' => $rating, 'stars' => 5, 'tag' => 'vote')),
   );
2
  • This loads the 5 stars, but the required CSS and JavaScript are not loaded.
    – Citricguy
    Apr 1, 2012 at 8:54
  • Code is changed due to last comment.
    – Nikit
    Apr 1, 2012 at 22:51
4

It can be done using the following code.

$form['presentation_ratting'] = array(
  '#type' => 'fivestar',
  '#stars' => 5,
  '#title' => t('Presentation.'),
);
3
  • 2
    Hello and welcome. Please avoid code-only answers and add a bit of explanation why and how this code would work.
    – Mołot
    Feb 5, 2014 at 7:58
  • This works for me. THX ! Dec 15, 2014 at 13:32
  • Working fine I need one thing more. How can I add default value? Apr 9, 2016 at 12:14

Your Answer

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

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