Same name and namespace in other branches
  1. 3.0.x src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatch.php \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch::viewElements()
1 method overrides ColorFieldFormatterSwatch::viewElements()
ColorFieldFormatterSwatchOptions::viewElements dans src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatchOptions.php

Fichier

src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatch.php, line 141

Classe

ColorFieldFormatterSwatch
Plugin implementation of the color_field swatch formatter.

Namespace

Drupal\color_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    $settings = $this->getSettings();
    $elements = [];
    $elements['#attached']['library'][] = 'color_field/color-field-formatter-swatch';
    foreach ($items as $delta => $item) {
        $elements[$delta] = [
            '#theme' => 'color_field_formatter_swatch',
            '#color' => $this->viewValue($item),
            '#shape' => $settings['shape'],
            '#width' => is_numeric($settings['width']) ? "{$settings['width']}px" : $settings['width'],
            '#height' => is_numeric($settings['height']) ? "{$settings['height']}px" : $settings['height'],
            '#attributes' => new Attribute([
                'class' => [
                    'color_field__swatch',
                    "color_field__swatch--{$settings['shape']}",
                ],
            ]),
        ];
        if ($settings['data_attribute']) {
            $color = new ColorHex($item->color, $item->opacity);
            $elements[$delta]['#attributes']['data-color'] = $color->toString(FALSE);
        }
    }
    return $elements;
}