public function ColorFieldTextFormatter::viewElements in Color Field 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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ ColorFieldTextFormatter.php, line 83 - Contains Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldTextFormatter.
Class
- ColorFieldTextFormatter
- Plugin implementation of the 'color_field_swatch' formatter.
Namespace
Drupal\color_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items) {
$format = $this
->getSetting('format');
$opacity = $this
->getFieldSetting('opacity');
foreach ($items as $delta => $item) {
switch ($format) {
case 'hexadecimal':
$output = '#' . $item->color;
break;
case 'rgb':
$color = color_field_hex2rgb($item->color);
$output = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ')';
break;
case 'rgba':
$color = color_field_hex2rgb($item->color);
$item->opacity = $opacity ? $item->color : 1;
$output = 'rgb(' . $color['r'] . ',' . $color['g'] . ',' . $color['b'] . ',' . $item->color . ')';
break;
}
$elements[$delta] = array(
'#markup' => $output,
);
}
return $elements;
}