SimpleTextFormatter.php

Same filename in this branch
  1. 3.x modules/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php
Same filename in other branches
  1. 4.0.x modules/field_permission_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php
  2. 4.0.x modules/field_example/src/Plugin/Field/FieldFormatter/SimpleTextFormatter.php

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;

/**
 * Plugin implementation of the 'field_example_simple_text' formatter.
 *
 * @FieldFormatter(
 *   id = "field_example_simple_text",
 *   module = "field_example",
 *   label = @Translation("Simple text-based formatter"),
 *   field_types = {
 *     "field_example_rgb"
 *   }
 * )
 */
class SimpleTextFormatter extends FormatterBase {
    
    /**
     * {@inheritdoc}
     */
    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;
    }

}

Classes

Title Deprecated Summary
SimpleTextFormatter Plugin implementation of the 'field_example_simple_text' formatter.