You are here

public function FractionDecimalFormatter::viewElements in Fraction 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/FractionDecimalFormatter.php \Drupal\fraction\Plugin\Field\FieldFormatter\FractionDecimalFormatter::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

1 method overrides FractionDecimalFormatter::viewElements()
PercentageFormatter::viewElements in src/Plugin/Field/FieldFormatter/PercentageFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/FractionDecimalFormatter.php, line 79

Class

FractionDecimalFormatter
Plugin implementation of the 'fraction_decimal' formatter.

Namespace

Drupal\fraction\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  // Output fraction as a decimal with a fixed or automatic precision.
  $precision = $this
    ->getSetting('precision');
  $auto_precision = !empty($this
    ->getSetting('auto_precision')) ? TRUE : FALSE;

  // Iterate through the items.
  foreach ($items as $delta => $item) {

    /** @var \Drupal\fraction\FractionInterface $fraction */
    $fraction = $item->fraction;
    $output = $fraction
      ->toDecimal($precision, $auto_precision);
    $elements[$delta] = [
      '#markup' => $this
        ->viewOutput($item, $output),
    ];
  }
  return $elements;
}