You are here

public function PricePlainFormatter::viewElements in Price 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldFormatter/PricePlainFormatter.php \Drupal\price\Plugin\Field\FieldFormatter\PricePlainFormatter::viewElements()
  2. 2.0.x src/Plugin/Field/FieldFormatter/PricePlainFormatter.php \Drupal\price\Plugin\Field\FieldFormatter\PricePlainFormatter::viewElements()
  3. 2.x src/Plugin/Field/FieldFormatter/PricePlainFormatter.php \Drupal\price\Plugin\Field\FieldFormatter\PricePlainFormatter::viewElements()
  4. 3.0.x src/Plugin/Field/FieldFormatter/PricePlainFormatter.php \Drupal\price\Plugin\Field\FieldFormatter\PricePlainFormatter::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/PricePlainFormatter.php, line 78

Class

PricePlainFormatter
Plugin implementation of the 'price_price_plain' formatter.

Namespace

Drupal\price\Plugin\Field\FieldFormatter

Code

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' => 'price_plain',
      '#number' => $item->number,
      '#currency' => $currencies[$item->currency_code],
      '#cache' => [
        'contexts' => [
          'languages:' . LanguageInterface::TYPE_INTERFACE,
          'price_country',
        ],
      ],
    ];
  }
  return $elements;
}