View source
<?php
namespace Drupal\range\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
class RangeDecimalItem extends RangeItemBase {
public static function defaultStorageSettings() {
return [
'precision' => 10,
'scale' => 2,
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
return static::propertyDefinitionsByType('string');
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$element['min']['#step'] = $element['max']['#step'] = pow(0.1, $this->definition
->getSetting('scale'));
return $element;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$precision_range = range(10, 32);
$scale_range = range(0, 10);
$element['precision'] = [
'#type' => 'select',
'#title' => $this
->t('Precision'),
'#options' => array_combine($precision_range, $precision_range),
'#default_value' => $this
->getSetting('precision'),
'#description' => $this
->t('The total number of digits to store in the database, including those to the right of the decimal.'),
'#disabled' => $has_data,
];
$element['scale'] = [
'#type' => 'select',
'#title' => $this
->t('Scale', [], [
'decimal places',
]),
'#options' => array_combine($scale_range, $scale_range),
'#default_value' => $this
->getSetting('scale'),
'#description' => $this
->t('The number of digits to the right of the decimal.'),
'#disabled' => $has_data,
];
return $element;
}
public function preSave() {
$this->from = round($this->from, $this
->getSetting('scale'));
$this->to = round($this->to, $this
->getSetting('scale'));
}
public static function getColumnSpecification(FieldStorageDefinitionInterface $field_definition) {
return [
'type' => 'numeric',
'precision' => $field_definition
->getSetting('precision'),
'scale' => $field_definition
->getSetting('scale'),
];
}
}