3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FormatterBase;
7 use Drupal\Core\Form\FormStateInterface;
10 * Plugin implementation of the '{{ plugin_label }}' formatter.
13 * id = "{{ plugin_id }}",
14 * label = @Translation("{{ plugin_label }}"),
20 class {{ class }} extends FormatterBase {
25 public static function defaultSettings() {
29 ] + parent::defaultSettings();
35 public function settingsForm(array $form, FormStateInterface $form_state) {
36 $settings = $this->getSettings();
38 $elements['prefix'] = [
39 '#type' => 'textfield',
40 '#title' => $this->t('Prefix'),
41 '#default_value' => $settings['prefix'],
44 $elements['suffix'] = [
45 '#type' => 'textfield',
46 '#title' => $this->t('Suffix'),
47 '#default_value' => $settings['suffix'],
56 public function settingsSummary() {
57 $settings = $this->getSettings();
60 if ($settings['prefix']) {
61 $summary[] = $this->t('Prefix: @prefix', ['@prefix' => $settings['prefix']]);
63 if ($settings['suffix']) {
64 $summary[] = $this->t('Suffix: @suffix', ['@suffix' => $settings['suffix']]);
73 public function viewElements(FieldItemListInterface $items, $langcode) {
75 $settings = $this->getSettings();
77 foreach ($items as $delta => $item) {
80 '#markup' => $item->value,
81 '#field_prefix' => $settings['prefix'],
82 '#field_suffix' => $settings['suffix'],