You are here

public function PlainStringFormatter::viewElements in String field formatter 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/PlainStringFormatter.php \Drupal\string_field_formatter\Plugin\Field\FieldFormatter\PlainStringFormatter::viewElements()
  2. 8.x src/Plugin/Field/FieldFormatter/PlainStringFormatter.php \Drupal\string_field_formatter\Plugin\Field\FieldFormatter\PlainStringFormatter::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 StringFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/PlainStringFormatter.php, line 107

Class

PlainStringFormatter
Plugin implementation of the 'plain_string_formatter' formatter.

Namespace

Drupal\string_field_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $wrapTag = $this
    ->getSetting('wrap_tag');
  if ($wrapTag === static::$wrapTagEmptyValue) {
    return $elements;
  }
  $attributes = [
    'class' => $this
      ->prepareClasses($this
      ->getSetting('wrap_class')),
  ];
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#type' => 'html_tag',
      '#tag' => $wrapTag,
      '#attributes' => $attributes,
      'content' => $elements[$delta],
    ];
  }
  return $elements;
}