Namespace
Drupal\field_example\Plugin\Field\FieldFormatter
File
-
modules/field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php
View source
<?php
namespace Drupal\field_example\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
class SimpleTextFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#attributes' => [
'style' => 'color: ' . $item->value,
],
'#value' => $this->t('The color code in this field is @code', [
'@code' => $item->value,
]),
];
}
return $elements;
}
}
Classes
| Title |
Deprecated |
Summary |
| SimpleTextFormatter |
|
Plugin implementation of the 'field_example_simple_text' formatter. |