You are here

public function TextWithTitleFormatter::viewElements in Text with Title Field 8

Define how the field type is displayed.

Inside this method we can customize how the field is displayed inside pages.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/TextWithTitleFormatter.php, line 27

Class

TextWithTitleFormatter
Plugin implementation of the 'TextWithTitleDefaultFieldFormatter' formatter.

Namespace

Drupal\text_with_title\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      'title' => [
        '#plain_text' => $item->title,
      ],
      'text' => [
        '#type' => 'processed_text',
        '#text' => $item->text['value'],
        '#format' => $item->text['format'],
        '#langcode' => $langcode,
      ],
    ];
  }
  return $elements;
}