Files
farmOS-dev/web/modules/fraction/src/Plugin/Field/FieldWidget/FractionDecimalWidget.php
2024-12-10 15:08:16 +01:00

124 lines
3.9 KiB
PHP

<?php
namespace Drupal\fraction\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'fraction_decimal' widget.
*
* @FieldWidget(
* id = "fraction_decimal",
* label = @Translation("Decimal"),
* field_types = {
* "fraction"
* }
* )
*/
class FractionDecimalWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'precision' => 0,
'auto_precision' => TRUE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
// Decimal precision.
$elements['precision'] = [
'#type' => 'number',
'#title' => $this->t('Decimal precision'),
'#description' => $this->t('Specify the number of digits after the decimal place to display when converting the fraction to a decimal. When "Auto precision" is enabled, this value essentially becomes a minimum fallback precision.'),
'#default_value' => $this->getSetting('precision'),
'#required' => TRUE,
'#weight' => 0,
];
// Auto precision.
$elements['auto_precision'] = [
'#type' => 'checkbox',
'#title' => $this->t('Auto precision'),
'#description' => $this->t('Automatically determine the maximum precision if the fraction has a base-10 denominator. For example, 1/100 would have a precision of 2, 1/1000 would have a precision of 3, etc.'),
'#default_value' => $this->getSetting('auto_precision'),
'#weight' => 1,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
// Summarize the precision setting.
$precision = $this->getSetting('precision');
$auto_precision = !empty($this->getSetting('auto_precision')) ? 'On' : 'Off';
$summary[] = $this->t('Precision: @precision, Auto-precision: @auto_precision', [
'@precision' => $precision,
'@auto_precision' => $auto_precision,
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Add a 'decimal' textfield for capturing the decimal value.
// The default value is converted to a decimal with the specified precision.
$precision = $this->getSetting('precision');
$auto_precision = !empty($this->getSetting('auto_precision')) ? TRUE : FALSE;
$element['decimal'] = $element + [
'#type' => 'fraction_decimal',
'#default_value' => $items->isEmpty() ? '' : $items[$delta]->fraction->toDecimal($precision, $auto_precision),
];
$field_settings = $this->getFieldSettings();
// Set minimum and maximum.
if (isset($field_settings['min']) && is_numeric($field_settings['min'])) {
$element['#min'] = $field_settings['min'];
}
if (isset($field_settings['max']) && is_numeric($field_settings['max'])) {
$element['#max'] = $field_settings['max'];
}
// Add prefix and suffix.
if ($field_settings['prefix']) {
$prefixes = explode('|', $field_settings['prefix']);
$element['decimal']['#field_prefix'] = FieldFilteredMarkup::create(array_pop($prefixes));
}
if ($field_settings['suffix']) {
$suffixes = explode('|', $field_settings['suffix']);
$element['decimal']['#field_suffix'] = FieldFilteredMarkup::create(array_pop($suffixes));
}
return $element;
}
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$value) {
// Remove the fraction form element wrapper if it exists to make the
// FractionItem::setValue method more consistent.
$value = $value['decimal'] ?? $value;
}
return $values;
}
}