You are here

public function RangeFormatterBase::viewElements in Range 8

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/RangeFormatterBase.php, line 120

Class

RangeFormatterBase
Parent plugin for decimal and integer range formatters.

Namespace

Drupal\range\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $from_value = $this
      ->formatNumber($item->from);
    $to_value = $this
      ->formatNumber($item->to);

    // Combine values if they are equal.
    if ($this
      ->getSetting('range_combine') && $from_value === $to_value) {
      $output = $this
        ->viewElementCombined($item, $from_value);
    }
    else {
      $output = $this
        ->viewElementSeparate($item, $from_value, $to_value);
    }
    if ($this
      ->getSetting('field_prefix_suffix')) {
      $output['#field_prefix'] = FieldFilteredMarkup::create($this
        ->getFieldSetting('field')['prefix']);
      $output['#field_suffix'] = FieldFilteredMarkup::create($this
        ->getFieldSetting('field')['suffix']);
    }
    $elements[$delta] = $output;
  }
  return $elements;
}