class SimpleTextFormatter

Plugin implementation of the 'field_example_simple_text' formatter.

Plugin annotation


@FieldFormatter(
  id = "field_example_simple_text",
  module = "field_example",
  label = @Translation("Simple text-based formatter"),
  field_types = {
    "field_example_rgb"
  }
)

Hierarchy

  • class \Drupal\field_example\Plugin\Field\FieldFormatter\SimpleTextFormatter extends \Drupal\Core\Field\FormatterBase

Expanded class hierarchy of SimpleTextFormatter

File

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

Namespace

Drupal\field_example\Plugin\Field\FieldFormatter
View source
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;
  }

}

Members

Title Sort descending Modifiers Object type Summary
SimpleTextFormatter::viewElements public function