View source
<?php
namespace Drupal\computed_field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
class ComputedDecimalItem extends ComputedFieldItemBase {
public static function defaultStorageSettings() {
return [
'precision' => 10,
'scale' => 2,
] + parent::defaultStorageSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Decimal value'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$settings = $field_definition
->getSettings();
$schema = [
'columns' => [
'value' => [
'type' => 'numeric',
'precision' => $settings['precision'],
'scale' => $settings['scale'],
],
],
];
return $schema;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['value'] = 0;
return $values;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$settings = $this
->getSettings();
$element['code']['#title'] = $this
->t('Code (PHP) to compute the <em>decimal</em> value');
$element['code']['#description'] .= '<p>' . t('The value will be rounded to @scale decimals.', [
'@scale' => $settings['scale'],
]) . '</p>';
return $element;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
$settings = $this
->getSettings();
$precision_range = range(10, 32);
$element['precision'] = [
'#type' => 'select',
'#title' => t('Precision'),
'#options' => array_combine($precision_range, $precision_range),
'#default_value' => $settings['precision'],
'#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'),
'#disabled' => $has_data,
];
$scale_range = range(0, 10);
$element['scale'] = [
'#type' => 'select',
'#title' => t('Scale', [], [
'context' => 'decimal places',
]),
'#options' => array_combine($scale_range, $scale_range),
'#default_value' => $settings['scale'],
'#description' => t('The number of digits to the right of the decimal.'),
'#disabled' => $has_data,
];
return $element;
}
public function executeCode() {
return round(parent::executeCode(), $this
->getSettings()['scale']);
}
}