public function DimensionsDefaultFormatter::viewElements in Physical Fields 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ DimensionsDefaultFormatter.php, line 24
Class
- DimensionsDefaultFormatter
- Plugin implementation of the 'physical_dimensions_default' formatter.
Namespace
Drupal\physical\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$unit_labels = LengthUnit::getLabels();
$element = [];
/** @var \Drupal\physical\Plugin\Field\FieldType\DimensionsItem $item */
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;
}