Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / field / formatter.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FormatterBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the '{{ plugin_label }}' formatter.
11  *
12  * @FieldFormatter(
13  *   id = "{{ plugin_id }}",
14  *   label = @Translation("{{ plugin_label }}"),
15  *   field_types = {
16  *     "string"
17  *   }
18  * )
19  */
20 class {{ class }} extends FormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'prefix' => '',
28       'suffix' => '',
29     ] + parent::defaultSettings();
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function settingsForm(array $form, FormStateInterface $form_state) {
36     $settings = $this->getSettings();
37
38     $elements['prefix'] = [
39       '#type' => 'textfield',
40       '#title' => $this->t('Prefix'),
41       '#default_value' => $settings['prefix'],
42     ];
43
44     $elements['suffix'] = [
45       '#type' => 'textfield',
46       '#title' => $this->t('Suffix'),
47       '#default_value' => $settings['suffix'],
48     ];
49
50     return $elements;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function settingsSummary() {
57     $settings = $this->getSettings();
58     $summary = [];
59
60     if ($settings['prefix']) {
61       $summary[] = $this->t('Prefix: @prefix', ['@prefix' => $settings['prefix']]);
62     }
63     if ($settings['suffix']) {
64       $summary[] = $this->t('Suffix: @suffix', ['@suffix' => $settings['suffix']]);
65     }
66
67     return $summary;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function viewElements(FieldItemListInterface $items, $langcode) {
74     $element = [];
75     $settings = $this->getSettings();
76
77     foreach ($items as $delta => $item) {
78       $element[$delta] = [
79         '#type' => 'item',
80         '#markup' => $item->value,
81         '#field_prefix' => $settings['prefix'],
82         '#field_suffix' => $settings['suffix'],
83       ];
84     }
85
86     return $element;
87   }
88
89 }