MeasurementDefaultFormatter.php in Physical Fields 8
File
src/Plugin/Field/FieldFormatter/MeasurementDefaultFormatter.php
View source
<?php
namespace Drupal\physical\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\physical\MeasurementType;
class MeasurementDefaultFormatter extends PhysicalFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$measurement_type = $this->fieldDefinition
->getSetting('measurement_type');
$unit_class = MeasurementType::getUnitClass($measurement_type);
$unit_labels = $unit_class::getLabels();
$element = [];
foreach ($items as $delta => $item) {
$number = $this->numberFormatter
->format($item->number);
$unit = isset($unit_labels[$item->unit]) ? $unit_labels[$item->unit] : $item->unit;
$element[$delta] = [
'#markup' => $number . ' ' . $unit,
];
}
return $element;
}
}