custom_field-1.0.x-dev/src/Plugin/CustomField/FieldWidget/DecimalWidget.php
src/Plugin/CustomField/FieldWidget/DecimalWidget.php
<?php
declare(strict_types=1);
namespace Drupal\custom_field\Plugin\CustomField\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\custom_field\Attribute\CustomFieldWidget;
use Drupal\custom_field\Plugin\CustomFieldTypeInterface;
/**
* Plugin implementation of the 'decimal' widget.
*/
#[CustomFieldWidget(
id: 'decimal',
label: new TranslatableMarkup('Decimal'),
category: new TranslatableMarkup('Number'),
field_types: [
'decimal',
],
)]
class DecimalWidget extends NumberWidgetBase {
/**
* {@inheritdoc}
*/
public function widget(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state, CustomFieldTypeInterface $field): array {
$element = parent::widget($items, $delta, $element, $form, $form_state, $field);
$element['#step'] = pow(0.1, $field->getScale());
return $element;
}
/**
* {@inheritdoc}
*/
public function massageFormValue(mixed $value, $column): mixed {
if (!is_numeric($value)) {
return NULL;
}
return $value;
}
}
