diff --git a/src/Plugin/Field/FieldWidget/DimensionsDefaultWidget.php b/src/Plugin/Field/FieldWidget/DimensionsDefaultWidget.php index 18c0866..8021b6f 100644 --- a/src/Plugin/Field/FieldWidget/DimensionsDefaultWidget.php +++ b/src/Plugin/Field/FieldWidget/DimensionsDefaultWidget.php @@ -36,6 +36,9 @@ class DimensionsDefaultWidget extends PhysicalWidgetBase { '#allow_unit_change' => $this->getSetting('allow_unit_change'), '#default_value' => $items[$delta]->getValue(), ] + $element; + if (array_filter($this->getSetting('allowed_units'))) { + $element['#available_units'] = $this->getSetting('allowed_units'); + } if (!$this->getSetting('allow_unit_change')) { $element['#available_units'] = [$default_unit]; } diff --git a/src/Plugin/Field/FieldWidget/MeasurementDefaultWidget.php b/src/Plugin/Field/FieldWidget/MeasurementDefaultWidget.php index 6aae62e..e47d522 100644 --- a/src/Plugin/Field/FieldWidget/MeasurementDefaultWidget.php +++ b/src/Plugin/Field/FieldWidget/MeasurementDefaultWidget.php @@ -35,6 +35,9 @@ class MeasurementDefaultWidget extends PhysicalWidgetBase { '#allow_unit_change' => $this->getSetting('allow_unit_change'), '#default_value' => $items[$delta]->getValue(), ] + $element; + if (array_filter($this->getSetting('allowed_units'))) { + $element['#available_units'] = $this->getSetting('allowed_units'); + } if (!$this->getSetting('allow_unit_change')) { $element['#available_units'] = [$default_unit]; } diff --git a/src/Plugin/Field/FieldWidget/PhysicalWidgetBase.php b/src/Plugin/Field/FieldWidget/PhysicalWidgetBase.php index 38ffc46..ccf9aea 100644 --- a/src/Plugin/Field/FieldWidget/PhysicalWidgetBase.php +++ b/src/Plugin/Field/FieldWidget/PhysicalWidgetBase.php @@ -17,6 +17,7 @@ abstract class PhysicalWidgetBase extends WidgetBase { return [ 'default_unit' => '', 'allow_unit_change' => TRUE, + 'allowed_units' => [], ] + parent::defaultSettings(); } @@ -36,6 +37,18 @@ abstract class PhysicalWidgetBase extends WidgetBase { '#default_value' => $this->getSetting('allow_unit_change'), ]; + /** @var \Drupal\physical\UnitInterface $unit_class */ + $unit_class = $this->getUnitClass(); + $unit_class::getLabels(); + + $element['allowed_units'] = [ + '#title' => $this->t('Allowed units'), + '#description' => $this->t('Select the units to display, selecting none will display all.'), + '#type' => 'checkboxes', + '#options' => $unit_class::getLabels(), + '#default_value' => $this->getSetting('allowed_units') ?: [], + ]; + return $element; } @@ -87,8 +100,8 @@ abstract class PhysicalWidgetBase extends WidgetBase { /** * Gets the unit class for the current field. * - * @return string - * The fully qualified class name. + * @return \Drupal\physical\UnitInterface + * The unit class. */ abstract protected function getUnitClass();