Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / field / widget.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Defines the '{{ plugin_id }}' field widget.
11  *
12  * @FieldWidget(
13  *   id = "{{ plugin_id }}",
14  *   label = @Translation("{{ plugin_label }}"),
15  *   field_types = {"string"},
16  * )
17  */
18 class {{ class }} extends WidgetBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static function defaultSettings() {
24     return [
25       'size' => 60,
26       'placeholder' => '',
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     $element['size'] = [
39       '#type' => 'number',
40       '#title' => $this->t('Size of textfield'),
41       '#default_value' => $settings['size'],
42       '#required' => TRUE,
43       '#min' => 1,
44     ];
45     $element['placeholder'] = [
46       '#type' => 'textfield',
47       '#title' => $this->t('Placeholder'),
48       '#default_value' => $settings['placeholder'],
49       '#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
50     ];
51     $element['prefix'] = [
52       '#type' => 'textfield',
53       '#title' => $this->t('Prefix'),
54       '#default_value' => $settings['prefix'],
55     ];
56     $element['suffix'] = [
57       '#type' => 'textfield',
58       '#title' => $this->t('Suffix'),
59       '#default_value' => $settings['suffix'],
60     ];
61
62     return $element;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function settingsSummary() {
69     $settings = $this->getSettings();
70     $summary[] = $this->t('Textfield size: @size', ['@size' => $settings['size']]);
71
72     if ($settings['placeholder']) {
73       $summary[] = $this->t('Placeholder: @placeholder', ['@placeholder' => $settings['placeholder']]);
74     }
75     if ($settings['prefix']) {
76       $summary[] = $this->t('Prefix: @prefix', ['@prefix' => $settings['prefix']]);
77     }
78     if ($settings['suffix']) {
79       $summary[] = $this->t('Suffix: @suffix', ['@suffix' => $settings['suffix']]);
80     }
81
82     return $summary;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
89
90     $settings = $this->getSettings();
91     $element['value'] = $element + [
92       '#type' => 'textfield',
93       '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
94       '#size' => $settings['size'],
95       '#placeholder' => $settings['placeholder'],
96       '#maxlength' => $this->getFieldSetting('max_length'),
97       '#field_prefix' => $settings['prefix'],
98       '#field_suffix' => $settings['suffix'],
99     ];
100
101     return $element;
102   }
103
104 }