DimensionsDefaultFormatter.php in Physical Fields 8
File
src/Plugin/Field/FieldFormatter/DimensionsDefaultFormatter.php
View source
<?php
namespace Drupal\physical\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\physical\LengthUnit;
class DimensionsDefaultFormatter extends PhysicalFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$unit_labels = LengthUnit::getLabels();
$element = [];
foreach ($items as $delta => $item) {
$dimensions = [
$this->numberFormatter
->format($item->length),
$this->numberFormatter
->format($item->width),
$this->numberFormatter
->format($item->height),
];
$unit = isset($unit_labels[$item->unit]) ? $unit_labels[$item->unit] : $item->unit;
$element[$delta] = [
'#markup' => implode(' × ', $dimensions) . ' ' . $unit,
];
}
return $element;
}
}