public function PricePlainFormatter::viewElements in Commerce Core 8.2
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
- modules/
price/ src/ Plugin/ Field/ FieldFormatter/ PricePlainFormatter.php, line 78
Class
- PricePlainFormatter
- Plugin implementation of the 'commerce_price_plain' formatter.
Namespace
Drupal\commerce_price\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$currency_codes = [];
foreach ($items as $delta => $item) {
$currency_codes[] = $item->currency_code;
}
$currencies = $this->currencyStorage
->loadMultiple($currency_codes);
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#theme' => 'commerce_price_plain',
'#number' => $item->number,
'#currency' => $currencies[$item->currency_code],
'#cache' => [
'contexts' => [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'country',
],
],
];
}
return $elements;
}