You are here

public function ElementClassTrait::setElementClass in Element Class Formatter 8

Set the class on the element.

Parameters

array $elements: The original elements render array.

string $class: The class string.

\Drupal\Core\Field\FieldItemListInterface $items: The list of field items.

Return value

array The updated elements render array.

2 calls to ElementClassTrait::setElementClass()
ElementLinkClassTrait::viewElements in src/Plugin/Field/FieldFormatter/ElementLinkClassTrait.php
LinkAllyFormatter::viewElements in src/Plugin/Field/FieldFormatter/LinkAllyFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/ElementClassTrait.php, line 87

Class

ElementClassTrait
Trait ElementClassTrait.

Namespace

Drupal\element_class_formatter\Plugin\Field\FieldFormatter

Code

public function setElementClass(array $elements, $class, FieldItemListInterface $items) {
  foreach ($items as $delta => $item) {

    // Add class.
    if (!empty($class)) {
      $elements[$delta]['#options']['attributes']['class'][] = $class;
    }
  }
  return $elements;
}