function SimpleTextFormatter::viewElements

Same name in this branch
  1. 3.x modules/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_permission_example\Plugin\Field\FieldFormatter\SimpleTextFormatter::viewElements()
Same name and namespace in other branches
  1. 4.0.x modules/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_permission_example\Plugin\Field\FieldFormatter\SimpleTextFormatter::viewElements()
  2. 4.0.x modules/field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php \Drupal\field_example\Plugin\Field\FieldFormatter\SimpleTextFormatter::viewElements()

Overrides FormatterInterface::viewElements

File

modules/field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php, line 25

Class

SimpleTextFormatter
Plugin implementation of the 'field_example_simple_text' formatter.

Namespace

Drupal\field_example\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
        $elements[$delta] = [
            // We create a render array to produce the desired markup,
            // "<p style="color: #hexcolor">The color code ... #hexcolor</p>".
            // See theme_html_tag().
'#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;
}