DimensionsDefaultWidget.php in Physical Fields 8
File
src/Plugin/Field/FieldWidget/DimensionsDefaultWidget.php
View source
<?php
namespace Drupal\physical\Plugin\Field\FieldWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\physical\LengthUnit;
class DimensionsDefaultWidget extends PhysicalWidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$default_unit = $this
->getDefaultUnit();
if ($items[$delta]
->isEmpty()) {
$items[$delta]->length = NULL;
$items[$delta]->width = NULL;
$items[$delta]->height = NULL;
$items[$delta]->unit = $default_unit;
}
$element = [
'#type' => 'physical_dimensions',
'#allow_unit_change' => $this
->getSetting('allow_unit_change'),
'#default_value' => $items[$delta]
->getValue(),
] + $element;
if (!$this
->getSetting('allow_unit_change')) {
$element['#available_units'] = [
$default_unit,
];
}
return $element;
}
protected function getUnitClass() {
return LengthUnit::class;
}
}