Same name and namespace in other branches
  1. 8.x-2.x src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatch.php \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch::viewValue() 1 comment

View an individual field value.

Parameters

\Drupal\color_field\Plugin\Field\FieldType\ColorFieldType $item: The field.

Return value

string The field value as rgb/rgba string.

2 calls to ColorFieldFormatterSwatch::viewValue()
ColorFieldFormatterSwatch::viewElements in src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatch.php
ColorFieldFormatterSwatchOptions::viewElements in src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatchOptions.php

File

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

Class

ColorFieldFormatterSwatch
Plugin implementation of the color_field swatch formatter.

Namespace

Drupal\color_field\Plugin\Field\FieldFormatter

Code

protected function viewValue(ColorFieldType $item) : string {
    $opacity = $this->getFieldSetting('opacity');
    $settings = $this->getSettings();
    $color_hex = new ColorHex($item->color, $item->opacity);
    return $opacity && $settings['opacity'] ? $color_hex->toRgb()
        ->toString(TRUE) : $color_hex->toRgb()
        ->toString(FALSE);
}