You are here

protected function FractionFormatterBase::viewOutput in Fraction 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/FractionFormatterBase.php \Drupal\fraction\Plugin\Field\FieldFormatter\FractionFormatterBase::viewOutput()

Account for extra output such as prefixes and suffixes.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: The field item to evaluate.

string $output: The output of the field.

Return value

string The output with all relevant additions.

3 calls to FractionFormatterBase::viewOutput()
FractionDecimalFormatter::viewElements in src/Plugin/Field/FieldFormatter/FractionDecimalFormatter.php
Builds a renderable array for a field value.
FractionFormatter::viewElements in src/Plugin/Field/FieldFormatter/FractionFormatter.php
Builds a renderable array for a field value.
PercentageFormatter::viewElements in src/Plugin/Field/FieldFormatter/PercentageFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/FractionFormatterBase.php, line 65

Class

FractionFormatterBase
Base class for fraction formatters.

Namespace

Drupal\fraction\Plugin\Field\FieldFormatter

Code

protected function viewOutput(FieldItemInterface $item, $output = '') {
  $field_settings = $this
    ->getFieldSettings();

  // Account for prefix and suffix.
  if ($this
    ->getSetting('prefix_suffix')) {
    $prefixes = isset($field_settings['prefix']) ? array_map([
      'Drupal\\Core\\Field\\FieldFilteredMarkup',
      'create',
    ], explode('|', $field_settings['prefix'])) : [
      '',
    ];
    $suffixes = isset($field_settings['suffix']) ? array_map([
      'Drupal\\Core\\Field\\FieldFilteredMarkup',
      'create',
    ], explode('|', $field_settings['suffix'])) : [
      '',
    ];
    $prefix = count($prefixes) > 1 ? $this
      ->formatPlural($item->value, $prefixes[0], $prefixes[1]) : $prefixes[0];
    $suffix = count($suffixes) > 1 ? $this
      ->formatPlural($item->value, $suffixes[0], $suffixes[1]) : $suffixes[0];
    $output = $prefix . $output . $suffix;
  }
  return $output;
}