function NumberWidget::formElement
Same name in other branches
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget::formElement()
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget::formElement()
- 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php \Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget::formElement()
Overrides WidgetInterface::formElement
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldWidget/ NumberWidget.php, line 69
Class
- NumberWidget
- Plugin implementation of the 'number' widget.
Namespace
Drupal\Core\Field\Plugin\Field\FieldWidgetCode
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'),
];
// Set the step for floating point and decimal numbers.
switch ($this->fieldDefinition
->getType()) {
case 'decimal':
$element['#step'] = pow(0.1, $field_settings['scale']);
break;
case 'float':
$element['#step'] = 'any';
break;
}
// Set minimum and maximum.
if (is_numeric($field_settings['min'])) {
$element['#min'] = $field_settings['min'];
}
if (is_numeric($field_settings['max'])) {
$element['#max'] = $field_settings['max'];
}
// Add prefix and suffix.
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,
];
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.