public function DefaultNumberFormatter::viewElements

Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().

Overrides FormatterInterface::viewElements

File

drupal/core/modules/field/modules/number/lib/Drupal/number/Plugin/field/formatter/DefaultNumberFormatter.php, line 67
Definition of Drupal\number\Plugin\field\formatter\DefaultNumberFormatter.

Class

DefaultNumberFormatter
Parent plugin for decimal and integer formatters

Namespace

Drupal\number\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $elements = array();
  foreach ($items as $delta => $item) {
    $output = $this
      ->numberFormat($item['value']);

    // Account for prefix and suffix.
    if ($this
      ->getSetting('prefix_suffix')) {
      $prefixes = isset($instance['settings']['prefix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['prefix'])) : array(
        '',
      );
      $suffixes = isset($instance['settings']['suffix']) ? array_map('field_filter_xss', explode('|', $instance['settings']['suffix'])) : array(
        '',
      );
      $prefix = count($prefixes) > 1 ? format_plural($item['value'], $prefixes[0], $prefixes[1]) : $prefixes[0];
      $suffix = count($suffixes) > 1 ? format_plural($item['value'], $suffixes[0], $suffixes[1]) : $suffixes[0];
      $output = $prefix . $output . $suffix;
    }
    $elements[$delta] = array(
      '#markup' => $output,
    );
  }
  return $elements;
}