public static function Fivestar::process in Fivestar 8
Process callback: process fivestar element.
File
- src/
Element/ Fivestar.php, line 53
Class
- Fivestar
- Provides a fivestar form element.
Namespace
Drupal\fivestar\ElementCode
public static function process(array &$element, FormStateInterface $form_state, &$complete_form) {
$settings = $element['#settings'];
$values = $element['#values'];
$class[] = 'clearfix';
$title = 'it';
if (isset($settings['entity_id']) && isset($settings['entity_type'])) {
$entity_id = $settings['entity_id'];
$entity_type = $settings['entity_type'];
$entity_manager = \Drupal::entityTypeManager();
$entity = $entity_manager
->getStorage($entity_type)
->load($entity_id);
$title = $entity
->label();
}
elseif (isset($complete_form['#node'])) {
$title = $complete_form['#node']->title;
}
$options = [
'-' => t('Select rating'),
];
for ($i = 1; $i <= $element['#stars']; $i++) {
$this_value = ceil($i * 100 / $element['#stars']);
$options[$this_value] = t('Give @title @star/@count', [
'@title' => $title,
'@star' => $i,
'@count' => $element['#stars'],
]);
}
// Display clear button only if enabled.
if ($element['#allow_clear'] == TRUE) {
$options[0] = t('Cancel rating');
}
if (self::userCanVote($element)) {
$element['vote'] = [
'#type' => 'select',
'#description' => self::getElementDescription($element),
'#options' => $options,
'#rating' => $values['vote_average'],
'#required' => $element['#required'],
'#attributes' => $element['#attributes'],
'#default_value' => self::getElementDefaultValue($element),
'#weight' => -2,
'#ajax' => $element['#ajax'],
'#parents' => isset($element['#parents']) ? $element['#parents'] : [],
];
}
else {
$renderer = \Drupal::service('renderer');
$static_stars = [
'#theme' => 'fivestar_static',
'#rating' => self::getElementDefaultValue($element),
'#stars' => $element['#stars'],
'#vote_type' => $element['#vote_type'],
'#widget' => $element['#widget'],
];
$element_static = [
'#theme' => 'fivestar_static_element',
'#star_display' => $renderer
->render($static_stars),
'#title' => '',
'#description' => self::getElementDescription($element),
];
$element['vote_statistic'] = [
'#type' => 'markup',
'#markup' => $renderer
->render($element_static),
];
}
$class[] = "fivestar-{$settings['text_format']}-text";
switch ($settings['display_format']) {
case 'average':
$class[] = 'fivestar-average-stars';
break;
case 'user':
$class[] = 'fivestar-user-stars';
break;
case 'smart':
$class[] = 'fivestar-smart-stars ' . ($values['vote_user'] ? 'fivestar-user-stars' : 'fivestar-average-stars');
break;
case 'dual':
$class[] = 'fivestar-combo-stars';
$static_average = [
'#type' => 'fivestar_static',
'#rating' => $values['vote_average'],
'#stars' => $settings['stars'],
'#vote_type' => $settings['vote_type'],
'#widget' => $settings['widget'],
];
if ($settings['text_format'] != 'none') {
$static_description = [
'#type' => 'fivestar_summary',
'#average_rating' => $settings['text_format'] == 'user' ? NULL : (isset($values['vote_average']) ? $values['vote_average'] : 0),
'#votes' => isset($values['vote_count']) ? $values['vote_count'] : 0,
'#stars' => $settings['stars'],
];
}
else {
$static_description = ' ';
}
$element_static = [
'#type' => 'fivestar_static_element',
'#title' => '',
'#star_display' => $static_average,
'#description' => $static_description,
];
$element['average'] = [
'#type' => 'markup',
'#markup' => $element_static,
'#weight' => -1,
];
break;
}
$widget_name = mb_strtolower($element['#widget']['name']);
$widget_name_kebab = str_replace('_', '-', $widget_name);
$class[] = 'fivestar-form-item';
$class[] = 'fivestar-' . $widget_name_kebab;
if ($widget_name != 'default') {
$element['#attached']['library'][] = \Drupal::service('fivestar.widget_manager')
->getWidgetLibrary($widget_name);
}
$element['#prefix'] = '<div ' . new Attribute([
'class' => $class,
]) . '>';
$element['#suffix'] = '</div>';
$element['#attached']['library'][] = 'fivestar/fivestar.base';
return $element;
}