You are here

public function ElementListClassTrait::viewElements in Element Class Formatter 8

2 methods override ElementListClassTrait::viewElements()
ListStringListClassFormatter::viewElements in src/Plugin/Field/FieldFormatter/ListStringListClassFormatter.php
Builds a renderable array for a field value.
StringListClassFormatter::viewElements in src/Plugin/Field/FieldFormatter/StringListClassFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/ElementListClassTrait.php, line 66

Class

ElementListClassTrait
The ElementListClassTrait is used to turn multi-cardinality fields into lists with a class.

Namespace

Drupal\element_class_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  if ($items
    ->isEmpty()) {
    return [];
  }
  $elements = parent::viewElements($items, $langcode);
  $class = $this
    ->getSetting('class');
  $attributes = new Attribute();
  if (!empty($class)) {
    $attributes
      ->addClass($class);
  }
  return [
    [
      '#theme' => 'item_list',
      '#items' => $elements,
      '#list_type' => $this
        ->getSetting('list_type'),
      '#attributes' => $attributes
        ->toArray(),
    ],
  ];
}