You are here

public function HtmlFieldFormatterEntityReference::viewElements in HTML Formatter 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 EntityReferenceLabelFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/HtmlFieldFormatterEntityReference.php, line 58

Class

HtmlFieldFormatterEntityReference
Plugin implementation of the 'entity reference label' html field formatter.

Namespace

Drupal\html_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  foreach ($elements as $delta => $element) {
    if (isset($element['#plaintext'])) {
      $value = $element['#plaintext'];
    }
    else {
      $value = $elements[$delta];
    }
    $elements[$delta]['#theme'] = 'html_formatter';
    $elements[$delta]['#value'] = $value;
    $elements[$delta]['#tag'] = $this
      ->getSetting('tag');
    $elements[$delta]['#attributes']['class'] = $this
      ->getSetting('class');
  }
  return $elements;
}