ListFloatItem.php in Drupal 10
File
core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php
View source
<?php
namespace Drupal\options\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class ListFloatItem extends ListItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('float')
->setLabel(t('Float value'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'float',
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
protected function allowedValuesDescription() {
$description = '<p>' . t('The possible values this field can contain. Enter one value per line, in the format key|label.');
$description .= '<br/>' . t('The key is the stored value, and must be numeric. The label will be used in displayed values and edit forms.');
$description .= '<br/>' . t('The label is optional: if a line contains a single number, it will be used as key and label.');
$description .= '<br/>' . t('Lists of labels are also accepted (one label per line), only if the field does not hold any values yet. Numeric keys will be automatically generated from the positions in the list.');
$description .= '</p>';
$description .= '<p>' . t('Allowed HTML tags in labels: @tags', [
'@tags' => FieldFilteredMarkup::displayAllowedTags(),
]) . '</p>';
return $description;
}
protected static function extractAllowedValues($string, $has_data) {
$values = parent::extractAllowedValues($string, $has_data);
if ($values) {
$keys = array_keys($values);
$labels = array_values($values);
$keys = array_map(function ($key) {
return is_numeric($key) ? (string) (double) $key : $key;
}, $keys);
return array_combine($keys, $labels);
}
}
protected static function validateAllowedValue($option) {
if (!is_numeric($option)) {
return t('Allowed values list: each key must be a valid integer or decimal.');
}
}
public static function simplifyAllowedValues(array $structured_values) {
$values = [];
foreach ($structured_values as $item) {
if (is_array($item['label'])) {
$item['label'] = static::simplifyAllowedValues($item['label']);
}
$values[(string) (double) $item['value']] = $item['label'];
}
return $values;
}
protected static function castAllowedValue($value) {
return (double) $value;
}
}