function fivestar_custom_widget in Fivestar 6.2
Same name and namespace in other branches
- 5 fivestar.module \fivestar_custom_widget()
- 6 fivestar.module \fivestar_custom_widget()
- 7.2 fivestar.module \fivestar_custom_widget()
Form builder; Build a custom Fivestar rating widget with arbitrary settings.
This function is usually not called directly, instead call drupal_get_form('fivestar_custom_widget', $values, $settings) when wanting to display a widget.
Parameters
$form_state: The form state provided by Form API.
$values: An array of current vote values from 0 to 100, with the following array keys:
- user: The user's current vote.
- average: The average vote value.
- count: The total number of votes so far on this content.
$settings: An array of settings that configure the properties of the rating widget. Available keys for the settings include:
- content_type: The type of content which will be voted upon.
- content_id: The content ID which will be voted upon.
- stars: The number of stars to display in this widget, from 2 to 10. Defaults to 5.
- autosubmit: Whether the form should be submitted upon star selection. Defaults to TRUE.
- allow_clear: Whether or not to show the "Clear current vote" icon when showing the widget. Defaults to FALSE.
- required: Whether this field is required before the form can be submitted. Defaults to FALSE.
- feedback_enable: Toggles the option to show the "Vote is being saved" text while a vote is being registered through AJAX. Defaults to TRUE.
- labels_enable: Toggles the option to show the "Give it 2/5 stars" text while hovering over the stars with the mouse.
- labels: An array of labels to be used. The number of labels should match the number of stars.
- tag: The VotingAPI tag that will be registered by this widget. Defaults to "vote".
1 call to fivestar_custom_widget()
- fivestar_form in ./
fivestar.module - Create the fivestar form for the current item. Note that this is not an implementation of hook_form(). We should probably change the function to reflect that.
2 string references to 'fivestar_custom_widget'
- fivestar_views_widget_handler in ./
fivestar.module - Generic VotingAPI Views formatter for displaying rating widget.
- theme_fivestar_preview in includes/
fivestar.admin.inc
File
- ./
fivestar.module, line 828 - A simple n-star voting widget, usable in other forms.
Code
function fivestar_custom_widget(&$form_state, $values, $settings) {
$form = array(
'#attributes' => array(
'class' => 'fivestar-widget',
),
'#redirect' => FALSE,
'#theme' => 'fivestar_widget',
);
$form['#submit'][] = 'fivestar_form_submit';
if (isset($settings['content_type'])) {
$form['content_type'] = array(
'#type' => 'hidden',
'#value' => $settings['content_type'],
);
}
if (isset($settings['content_id'])) {
$form['content_id'] = array(
'#type' => 'hidden',
'#value' => $settings['content_id'],
);
}
if (isset($settings['tag'])) {
$form['tag'] = array(
'#type' => 'hidden',
'#value' => $settings['tag'],
);
}
$form['vote'] = array(
'#type' => 'fivestar',
'#stars' => $settings['stars'],
'#vote_count' => $values['count'],
'#vote_average' => $values['average'],
'#auto_submit' => isset($settings['autosubmit']) ? $settings['autosubmit'] : TRUE,
'#auto_submit_path' => !isset($settings['autosubmit']) || $settings['autosubmit'] ? 'fivestar/vote/' . $settings['content_type'] . '/' . $settings['content_id'] . '/' . $settings['tag'] : NULL,
'#allow_clear' => $settings['allow_clear'],
'#content_id' => isset($settings['content_id']) ? $settings['content_id'] : NULL,
'#required' => isset($settings['required']) ? $settings['required'] : FALSE,
'#feedback_enable' => isset($settings['feedback_enable']) ? $settings['feedback_enable'] : TRUE,
'#labels_enable' => isset($settings['labels_enable']) ? $settings['labels_enable'] : TRUE,
'#labels' => isset($settings['labels']) ? $settings['labels'] : NULL,
'#tag' => isset($settings['tag']) ? $settings['tag'] : 'vote',
);
$form['destination'] = array(
'#type' => 'hidden',
'#value' => $_GET['q'],
);
$form['fivestar_submit'] = array(
'#type' => 'submit',
'#value' => t('Rate'),
'#attributes' => array(
'class' => 'fivestar-submit',
),
);
$form['vote']['#attributes']['class'] = isset($form['vote']['#attributes']['class']) ? $form['vote']['#attributes']['class'] : '';
$settings['feedback_enable'] = isset($settings['feedback_enable']) ? $settings['feedback_enable'] : TRUE;
switch ($settings['text_display']) {
case 'user':
$form['vote']['#description'] = theme('fivestar_summary', $values['user'], NULL, $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-user-text';
break;
case 'average':
$form['vote']['#description'] = $settings['style'] == 'dual' ? NULL : theme('fivestar_summary', NULL, $values['average'], $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-average-text';
break;
case 'smart':
$form['vote']['#description'] = $settings['style'] == 'dual' && !$values['user'] ? NULL : theme('fivestar_summary', $values['user'], $values['user'] ? NULL : $values['average'], $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-smart-text ' . ($values['user'] ? 'fivestar-user-text' : 'fivestar-average-text');
break;
case 'dual':
$form['vote']['#description'] = theme('fivestar_summary', $values['user'], $settings['style'] == 'dual' ? NULL : $values['average'], $settings['style'] == 'dual' ? NULL : $values['count'], $settings['stars'], $settings['feedback_enable']);
$form['vote']['#attributes']['class'] .= ' fivestar-combo-text';
break;
}
switch ($settings['star_display']) {
case 'average':
$form['vote']['#title'] = t('Average');
$form['vote']['#default_value'] = $values['average'];
$form['vote']['#attributes']['class'] .= ' fivestar-average-stars';
break;
case 'user':
$form['vote']['#title'] = t('Your rating');
$form['vote']['#default_value'] = $values['user'];
$form['vote']['#attributes']['class'] .= ' fivestar-user-stars';
break;
case 'smart':
$form['vote']['#title'] = $values['user'] ? t('Your rating') : t('Average');
$form['vote']['#default_value'] = $values['user'] ? $values['user'] : $values['average'];
$form['vote']['#attributes']['class'] .= ' fivestar-smart-stars ' . ($values['user'] ? 'fivestar-user-stars' : 'fivestar-average-stars');
break;
case 'dual':
$form['vote']['#title'] = t('Your rating');
$form['vote']['#default_value'] = $values['user'];
$form['vote']['#attributes']['class'] .= ' fivestar-combo-stars';
$form['#attributes']['class'] .= ' fivestar-combo-stars';
$static_average = theme('fivestar_static', $values['average'], $settings['stars'], $settings['tag']);
if ($settings['text_display'] == 'none' && !$settings['labels_enable'] && !$settings['feedback_enable']) {
$static_description = NULL;
}
elseif ($settings['text_display'] != 'none') {
$static_description = theme('fivestar_summary', NULL, $settings['text_display'] == 'user' ? NULL : (isset($values['average']) ? $values['average'] : 0), isset($values['count']) ? $values['count'] : 0, $settings['stars'], FALSE);
}
else {
$static_description = ' ';
}
$form['average'] = array(
'#type' => 'markup',
'#value' => theme('fivestar_static_element', $static_average, $settings['title'] !== FALSE ? t('Average') : NULL, $static_description),
'#weight' => -1,
);
break;
}
// Set an over-ridding title if passed in.
// An empty title won't change the default, a string will set a new title,
// and title === FALSE will unset the title entirely.
if (isset($settings['title'])) {
if ($settings['title'] !== FALSE) {
$form['vote']['#title'] = $settings['title'];
}
else {
unset($form['vote']['#title']);
unset($form['average']['#title']);
}
}
elseif ($settings['tag'] && $settings['tag'] != 'vote') {
$form['vote']['#title'] .= ' (' . ucfirst($settings['tag']) . ')';
}
return $form;
}