function fivestar_expand in Fivestar 6.2
Same name and namespace in other branches
- 5 fivestar.module \fivestar_expand()
- 6 fivestar.module \fivestar_expand()
- 7.2 fivestar.module \fivestar_expand()
Process callback for fivestar_element -- see fivestar_element()
1 string reference to 'fivestar_expand'
- fivestar_elements in ./
fivestar.module - Implementation of hook_elements().
File
- ./
fivestar.module, line 1240 - A simple n-star voting widget, usable in other forms.
Code
function fivestar_expand($element) {
if (isset($element['#vote_count'])) {
$element['vote_count'] = array(
'#type' => 'hidden',
'#value' => $element['#vote_count'],
);
}
if (isset($element['#vote_average'])) {
$element['vote_average'] = array(
'#type' => 'hidden',
'#value' => $element['#vote_average'],
);
}
if ($element['#auto_submit'] && !empty($element['#auto_submit_path'])) {
$element['auto_submit_path'] = array(
'#type' => 'hidden',
'#value' => url($element['#auto_submit_path']),
'#attributes' => array(
'class' => 'fivestar-path',
),
);
$element['auto_submit_token'] = array(
'#type' => 'hidden',
'#value' => fivestar_get_token($element['#auto_submit_path']),
'#attributes' => array(
'class' => 'fivestar-token',
),
);
}
if (!isset($element['#default_value'])) {
$element['#default_value'] = 0;
}
$options = array(
'-' => t('Select rating'),
);
$default_value = $element['#default_value'];
for ($i = 0; $i <= $element['#stars']; $i++) {
$this_value = ceil($i * 100 / $element['#stars']);
$next_value = ceil(($i + 1) * 100 / $element['#stars']);
// Display clear button only if enabled.
if ($element['#allow_clear'] == TRUE && $i == 0) {
$options[$this_value] = isset($element['#labels'][$i]) ? t(filter_xss_admin($element['#labels'][$i])) : t('Cancel rating');
}
// Display a normal star value.
if ($i > 0) {
if (isset($element['#labels'][$i])) {
$options[$this_value] = $element['#labels'][$i] == '' ? $i : t(filter_xss_admin($element['#labels'][$i]), array(
'@star' => $i,
'@count' => $element['#stars'],
));
}
else {
$options[$this_value] = t('Give it @star/@count', array(
'@star' => $i,
'@count' => $element['#stars'],
));
}
}
// Round up the default value to the next exact star value if needed.
if ($this_value < $element['#default_value'] && $next_value > $element['#default_value']) {
$default_value = $next_value;
}
}
$element['vote'] = array(
'#type' => 'select',
'#options' => $options,
'#required' => $element['#required'],
'#default_value' => $default_value,
'#parents' => $element['#parents'],
'#theme' => 'fivestar_select',
'#weight' => $element['#weight'],
);
// If a default value is not exactly on a radio value, round up to the next one
if ($element['#default_value'] > $this_value && $element['#default_value'] <= $next_value) {
$element['vote']['#default_value'] = $next_value;
}
// Set a class for the display of label text on hover.
$label_class = $element['#labels_enable'] ? ' fivestar-labels-hover' : '';
$element['#prefix'] = '<div class="fivestar-form-item ' . (isset($element['#attributes']['class']) ? $element['#attributes']['class'] : '') . $label_class . '">';
$element['#suffix'] = '</div>';
// Add validation function that considers a 0 value as empty.
$element['#element_validate'] = array(
'fivestar_validate',
);
return $element;
}