NumberWidget.php in Drupal 10
File
core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php
View source
<?php
namespace Drupal\Core\Field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
class NumberWidget extends WidgetBase {
public static function defaultSettings() {
return [
'placeholder' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['placeholder'] = [
'#type' => 'textfield',
'#title' => t('Placeholder'),
'#default_value' => $this
->getSetting('placeholder'),
'#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$placeholder = $this
->getSetting('placeholder');
if (!empty($placeholder)) {
$summary[] = t('Placeholder: @placeholder', [
'@placeholder' => $placeholder,
]);
}
else {
$summary[] = t('No placeholder');
}
return $summary;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$value = $items[$delta]->value ?? NULL;
$field_settings = $this
->getFieldSettings();
$element += [
'#type' => 'number',
'#default_value' => $value,
'#placeholder' => $this
->getSetting('placeholder'),
];
switch ($this->fieldDefinition
->getType()) {
case 'decimal':
$element['#step'] = pow(0.1, $field_settings['scale']);
break;
case 'float':
$element['#step'] = 'any';
break;
}
if (is_numeric($field_settings['min'])) {
$element['#min'] = $field_settings['min'];
}
if (is_numeric($field_settings['max'])) {
$element['#max'] = $field_settings['max'];
}
if ($field_settings['prefix']) {
$prefixes = explode('|', $field_settings['prefix']);
$element['#field_prefix'] = FieldFilteredMarkup::create(array_pop($prefixes));
}
if ($field_settings['suffix']) {
$suffixes = explode('|', $field_settings['suffix']);
$element['#field_suffix'] = FieldFilteredMarkup::create(array_pop($suffixes));
}
return [
'value' => $element,
];
}
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
return $element['value'];
}
}