class ColorBackgroudFormatter
Same name in other branches
- 3.x modules/field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php \Drupal\field_example\Plugin\Field\FieldFormatter\ColorBackgroudFormatter
- 4.0.x modules/field_example/src/Plugin/Field/FieldFormatter/ColorBackgroudFormatter.php \Drupal\field_example\Plugin\Field\FieldFormatter\ColorBackgroudFormatter
Plugin implementation of the 'field_example_color_background' formatter.
Plugin annotation
@FieldFormatter(
id = "field_example_color_background",
label = @Translation("Change the background of the output text"),
field_types = {
"field_example_rgb"
}
)
Hierarchy
- class \Drupal\field_example\Plugin\Field\FieldFormatter\ColorBackgroudFormatter extends \Drupal\Core\Field\FormatterBase
Expanded class hierarchy of ColorBackgroudFormatter
File
-
field_example/
src/ Plugin/ Field/ FieldFormatter/ ColorBackgroudFormatter.php, line 19
Namespace
Drupal\field_example\Plugin\Field\FieldFormatterView source
class ColorBackgroudFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
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;
}
}