StarRatingItem.php in Star Rating 8.3
File
src/Plugin/Field/FieldType/StarRatingItem.php
View source
<?php
namespace Drupal\starrating\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Form\FormStateInterface;
class StarRatingItem extends FieldItemBase {
const DEFAULT_MAX_RATING_VALUE = 10;
public static function defaultFieldSettings() {
return [
'max_value' => self::DEFAULT_MAX_RATING_VALUE,
] + parent::defaultFieldSettings();
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
),
),
);
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('integer')
->setLabel(t('Star rating'));
return $properties;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['max_value'] = [
'#title' => $this
->t('Maximum rating value'),
'#type' => 'select',
'#options' => [
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
10 => '10',
],
'#default_value' => $this
->getSetting('max_value'),
];
return $element;
}
}