You are here

public function GeneralNumberFormatter::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

1 call to GeneralNumberFormatter::viewElements()
GeneralNumberWithBarIndicatorFormatter::viewElements in src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php
Builds a renderable array for a field value.
1 method overrides GeneralNumberFormatter::viewElements()
GeneralNumberWithBarIndicatorFormatter::viewElements in src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/GeneralNumberFormatter.php, line 847

Class

GeneralNumberFormatter
Format a number field with a variety of notation styles and parameters.

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) {
    $output = $this
      ->numberFormat($item->value);

    // Output the raw value in a content attribute if the text of the HTML
    // element differs from the raw value (for example when a prefix is used).
    if (isset($item->_attributes) === TRUE && $item->value !== $output) {
      $item->_attributes += [
        'content' => $item->value,
      ];
    }

    // The value may include HTML markup, so mark it as safe.
    $output = new FormattableMarkup($output, []);
    $elements[$delta] = [
      '#markup' => $output,
      '#attached' => [
        'library' => [
          'formatter_suite/formatter_suite.usage',
        ],
      ],
    ];
  }

  //
  // Add multi-value field processing.
  // ---------------------------------
  // If the field has multiple values, redirect to a theme and pass
  // the list style and separator.
  $isMultiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();
  if ($isMultiple === TRUE) {

    // Replace the 'field' theme with one that supports lists.
    $elements['#theme'] = 'formatter_suite_field_list';

    // Set the list style.
    $elements['#list_style'] = $this
      ->getSetting('listStyle');

    // Set the list separator.
    //
    // Security: The list separator is entered by an administrator.
    // It may legitimately include HTML entities and minor HTML, but
    // it should not include dangerous HTML.
    //
    // Because it may include HTML, we cannot pass it as-is and let a TWIG
    // template use {{ }}, which will process the text and corrupt any
    // entered HTML or HTML entities.
    //
    // We therefore use an Xss admin filter to remove any egreggious HTML
    // (such as scripts and styles), and then FormattableMarkup() to mark the
    // resulting text as safe.
    $listSeparator = Xss::filterAdmin($this
      ->getSetting('listSeparator'));
    $elements['#list_separator'] = new FormattableMarkup($listSeparator, []);
  }
  return $elements;
}