FractionWidget.php in Fraction 8
File
src/Plugin/Field/FieldWidget/FractionWidget.php
View source
<?php
namespace Drupal\fraction\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class FractionWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element += [
'#type' => 'details',
'#open' => TRUE,
];
$element['fraction'] = [
'#type' => 'fraction',
'#title' => $this
->t('Fraction'),
'#default_value' => [
'numerator' => isset($items[$delta]->numerator) ? $items[$delta]->numerator : NULL,
'denominator' => isset($items[$delta]->denominator) ? $items[$delta]->denominator : NULL,
],
];
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$value) {
$value = $value['fraction'] ?? $value;
}
return $values;
}
}