How do you add a FiveStar widget to a (non-node) form using hook_form_alter?
2 Answers
$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')),
);
-
This loads the 5 stars, but the required CSS and JavaScript are not loaded. Apr 1, 2012 at 8:54
-
It can be done using the following code.
$form['presentation_ratting'] = array(
'#type' => 'fivestar',
'#stars' => 5,
'#title' => t('Presentation.'),
);
-
2Hello and welcome. Please avoid code-only answers and add a bit of explanation why and how this code would work.– MołotFeb 5, 2014 at 7:58
-
-
Working fine I need one thing more. How can I add default value? Apr 9, 2016 at 12:14