ColorBackgroudFormatter.php in Examples for Developers 8
File
field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php
View source
<?php
namespace Drupal\field_example\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
class ColorBackgroudFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('The content area color has been changed to @code', [
'@code' => $item->value,
]),
'#attributes' => [
'style' => 'background-color: ' . $item->value,
],
];
}
return $elements;
}
}