<?php

declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;

{% apply sort_namespaces %}
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
  {% if configurable %}
use Drupal\Core\Form\FormStateInterface;
  {% endif %}
{% endapply %}

/**
 * Plugin implementation of the '{{ plugin_label }}' formatter.
 */
#[FieldFormatter(
  id: '{{ plugin_id }}',
  label: new TranslatableMarkup('{{ plugin_label }}'),
  field_types: ['string'],
)]
class {{ class }} extends FormatterBase {

{% if configurable %}
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings(): array {
    $setting = ['foo' => 'bar'];
    return $setting + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state): array {
    $elements['foo'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Foo'),
      '#default_value' => $this->getSetting('foo'),
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary(): array {
    return [
      $this->t('Foo: @foo', ['@foo' => $this->getSetting('foo')]),
    ];
  }

{% endif %}
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode): array {
    $element = [];
    foreach ($items as $delta => $item) {
      $element[$delta] = [
        '#markup' => $item->value,
      ];
    }
    return $element;
  }

}
