StarRatingWidget.php in Star Rating 8.3
File
src/Plugin/Field/FieldWidget/StarRatingWidget.php
View source
<?php
namespace Drupal\starrating\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class StarRatingWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$value = isset($items[$delta]->value) ? $items[$delta]->value : '';
$field_settings = $this
->getFieldSettings();
$max_value = $field_settings['max_value'];
$options = array();
$options['null'] = $this
->t('Not selected');
for ($i = 0; $i <= $max_value; $i++) {
$options[$i] = $i;
}
$element += array(
'#type' => 'select',
'#default_value' => NULL,
'#options' => $options,
'#default_value' => $value,
);
return array(
'value' => $element,
);
}
}