You are here

public function NumberWithBytesFormatter::viewElements in Formatter Suite 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/NumberWithBytesFormatter.php, line 236

Class

NumberWithBytesFormatter
Formats numbers with a byte suffix, like "bytes", "KB", or "MB".

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  if ($items
    ->isEmpty() === TRUE) {
    return [];
  }
  $this
    ->sanitizeSettings();
  $elements = [];
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#markup' => Utilities::formatBytes($item->value, $this
        ->getSetting('kunit'), $this
        ->getSetting('fullWord'), $this
        ->getSetting('decimalDigits')),
    ];
  }
  return $elements;
}