You are here

public function PriceDefaultFormatter::viewElements in Price 3.0.x

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

Class

PriceDefaultFormatter
Plugin implementation of the 'price_price_default' formatter.

Namespace

Drupal\price\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $options = $this
    ->getFormattingOptions();
  $elements = [];
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#markup' => $this->currencyFormatter
        ->format($item->number, $item->currency_code, $options),
      '#cache' => [
        'contexts' => [
          'languages:' . LanguageInterface::TYPE_INTERFACE,
          'price_country',
        ],
      ],
    ];
  }
  return $elements;
}