public function PriceModifiedDefaultFormatter::viewElements in Price 8
Same name and namespace in other branches
- 3.x src/Plugin/Field/FieldFormatter/PriceModifiedDefaultFormatter.php \Drupal\price\Plugin\Field\FieldFormatter\PriceModifiedDefaultFormatter::viewElements()
- 3.0.x src/Plugin/Field/FieldFormatter/PriceModifiedDefaultFormatter.php \Drupal\price\Plugin\Field\FieldFormatter\PriceModifiedDefaultFormatter::viewElements()
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/ PriceModifiedDefaultFormatter.php, line 169
Class
- PriceModifiedDefaultFormatter
- Plugin implementation of the 'price_modified' formatter.
Namespace
Drupal\price\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$currency_codes = [];
$modifier_codes = [];
foreach ($items as $delta => $item) {
$currency_codes[] = $item->currency_code;
$modifier_codes[] = $item->modifier;
}
$currencies = $currency_codes ? $this->currencyStorage
->loadMultiple($currency_codes) : [];
$modifiers = $modifier_codes ? $this->modifierStorage
->loadMultiple($modifier_codes) : [];
$elements = [];
foreach ($items as $delta => $item) {
$currency = $currencies[$item->currency_code];
$modifier = $modifiers[$item->modifier];
$elements[$delta] = [
'#markup' => ($modifier
->id() != 'empty' ? $modifier
->label() : '') . ' ' . $this->numberFormatter
->formatCurrency($item->number, $currency),
'#cache' => [
'contexts' => [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'price_country',
],
],
];
}
return $elements;
}