You are here

public function TextDefaultFormatter::viewElements in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/text/src/Plugin/Field/FieldFormatter/TextDefaultFormatter.php \Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter::viewElements()
  2. 9 core/modules/text/src/Plugin/Field/FieldFormatter/TextDefaultFormatter.php \Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter::viewElements()

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

core/modules/text/src/Plugin/Field/FieldFormatter/TextDefaultFormatter.php, line 26

Class

TextDefaultFormatter
Plugin implementation of the 'text_default' formatter.

Namespace

Drupal\text\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  // The ProcessedText element already handles cache context & tag bubbling.
  // @see \Drupal\filter\Element\ProcessedText::preRenderText()
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#type' => 'processed_text',
      '#text' => $item->value,
      '#format' => $item->format,
      '#langcode' => $item
        ->getLangcode(),
    ];
  }
  return $elements;
}