Same name and namespace in other branches
- 8.x-2.x src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterCss::viewElements()
Fichier
-
src/
Plugin/ Field/ FieldFormatter/ ColorFieldFormatterCss.php, line 220
Classe
- ColorFieldFormatterCss
- Plugin implementation of the color_field css declaration formatter.
Namespace
Drupal\color_field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) : array {
$settings = $this->getSettings();
$elements = [];
$tokens = [
$this->getTokenType() => $items->getEntity(),
];
foreach ($items as $item) {
$value = $this->viewValue($item);
$tokens['color_field'] = $item;
if ($settings['advanced']) {
$inline_css = $this->tokenService
->replace($settings['css'], $tokens);
}
else {
$selector = $this->tokenService
->replace($settings['selector'], $tokens);
$important = $settings['important'] ? ' !important' : '';
$property = $settings['property'];
$inline_css = $selector . ' { ' . $property . ': ' . $value . $important . '; }';
}
$elements['#attached']['html_head'][] = [
[
'#tag' => 'style',
'#value' => $inline_css,
],
sha1($inline_css),
];
// If rendered in a view entity field, the #attached only propagates if
// there is some markup set.
$elements[0] = [
'#markup' => "<div class='hidden'>{$value}</div>",
];
}
return $elements;
}