Chris@4
|
1 <?php
|
Chris@4
|
2
|
Chris@4
|
3 namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
|
Chris@4
|
4
|
Chris@4
|
5 {% sort %}
|
Chris@4
|
6 {% if datetime %}
|
Chris@4
|
7 use Drupal\Core\Datetime\DrupalDateTime;
|
Chris@4
|
8 {% endif %}
|
Chris@4
|
9 use Drupal\Core\Field\FieldItemListInterface;
|
Chris@4
|
10 use Drupal\Core\Field\WidgetBase;
|
Chris@4
|
11 use Drupal\Core\Form\FormStateInterface;
|
Chris@4
|
12 use Symfony\Component\Validator\ConstraintViolationInterface;
|
Chris@4
|
13 {% if list %}
|
Chris@4
|
14 use Drupal\{{ machine_name }}\Plugin\Field\FieldType\{{ type_class }};
|
Chris@4
|
15 {% endif %}
|
Chris@4
|
16 {% endsort %}
|
Chris@4
|
17
|
Chris@4
|
18 /**
|
Chris@4
|
19 * Defines the '{{ field_id }}' field widget.
|
Chris@4
|
20 *
|
Chris@4
|
21 * @FieldWidget(
|
Chris@4
|
22 * id = "{{ field_id }}",
|
Chris@4
|
23 * label = @Translation("{{ field_label }}"),
|
Chris@4
|
24 * field_types = {"{{ field_id }}"},
|
Chris@4
|
25 * )
|
Chris@4
|
26 */
|
Chris@4
|
27 class {{ widget_class }} extends WidgetBase {
|
Chris@4
|
28
|
Chris@4
|
29 {% if widget_settings %}
|
Chris@4
|
30 /**
|
Chris@4
|
31 * {@inheritdoc}
|
Chris@4
|
32 */
|
Chris@4
|
33 public static function defaultSettings() {
|
Chris@4
|
34 return ['foo' => 'bar'] + parent::defaultSettings();
|
Chris@4
|
35 }
|
Chris@4
|
36
|
Chris@4
|
37 /**
|
Chris@4
|
38 * {@inheritdoc}
|
Chris@4
|
39 */
|
Chris@4
|
40 public function settingsForm(array $form, FormStateInterface $form_state) {
|
Chris@4
|
41 $settings = $this->getSettings();
|
Chris@4
|
42 $element['foo'] = [
|
Chris@4
|
43 '#type' => 'textfield',
|
Chris@4
|
44 '#title' => $this->t('Foo'),
|
Chris@4
|
45 '#default_value' => $settings['foo'],
|
Chris@4
|
46 ];
|
Chris@4
|
47 return $element;
|
Chris@4
|
48 }
|
Chris@4
|
49
|
Chris@4
|
50 /**
|
Chris@4
|
51 * {@inheritdoc}
|
Chris@4
|
52 */
|
Chris@4
|
53 public function settingsSummary() {
|
Chris@4
|
54 $settings = $this->getSettings();
|
Chris@4
|
55 $summary[] = $this->t('Foo: @foo', ['@foo' => $settings['foo']]);
|
Chris@4
|
56 return $summary;
|
Chris@4
|
57 }
|
Chris@4
|
58
|
Chris@4
|
59 {% endif %}
|
Chris@4
|
60 /**
|
Chris@4
|
61 * {@inheritdoc}
|
Chris@4
|
62 */
|
Chris@4
|
63 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
Chris@4
|
64
|
Chris@4
|
65 {% for subfield in subfields %}
|
Chris@4
|
66 {% set title %}'#title' => $this->t('{{ subfield.name }}'),{% endset %}
|
Chris@4
|
67 {% set default_value %}'#default_value' => isset($items[$delta]->{{ subfield.machine_name }}) ? $items[$delta]->{{ subfield.machine_name }} : NULL,{% endset %}
|
Chris@4
|
68 {% set size %}'#size' => 20,{% endset %}
|
Chris@4
|
69 {% if subfield.list %}
|
Chris@4
|
70 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
71 '#type' => 'select',
|
Chris@4
|
72 {{ title }}
|
Chris@4
|
73 '#options' => ['' => $this->t('- {{ subfield.required ? 'Select a value' : 'None' }} -')] + {{ type_class }}::{{ subfield.allowed_values_method }}(),
|
Chris@4
|
74 {{ default_value }}
|
Chris@4
|
75 ];
|
Chris@4
|
76 {% else %}
|
Chris@4
|
77 {% if subfield.type == 'boolean' %}
|
Chris@4
|
78 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
79 '#type' => 'checkbox',
|
Chris@4
|
80 {{ title }}
|
Chris@4
|
81 {{ default_value }}
|
Chris@4
|
82 ];
|
Chris@4
|
83 {% elseif subfield.type == 'string' %}
|
Chris@4
|
84 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
85 '#type' => 'textfield',
|
Chris@4
|
86 {{ title }}
|
Chris@4
|
87 {{ default_value }}
|
Chris@4
|
88 {% if inline %}
|
Chris@4
|
89 {{ size }}
|
Chris@4
|
90 {% endif %}
|
Chris@4
|
91 ];
|
Chris@4
|
92 {% elseif subfield.type == 'text' %}
|
Chris@4
|
93 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
94 '#type' => 'textarea',
|
Chris@4
|
95 {{ title }}
|
Chris@4
|
96 {{ default_value }}
|
Chris@4
|
97 ];
|
Chris@4
|
98 {% elseif subfield.type == 'integer' %}
|
Chris@4
|
99 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
100 '#type' => 'number',
|
Chris@4
|
101 {{ title }}
|
Chris@4
|
102 {{ default_value }}
|
Chris@4
|
103 ];
|
Chris@4
|
104 {% elseif subfield.type == 'float' %}
|
Chris@4
|
105 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
106 '#type' => 'number',
|
Chris@4
|
107 {{ title }}
|
Chris@4
|
108 {{ default_value }}
|
Chris@4
|
109 '#step' => 0.001,
|
Chris@4
|
110 ];
|
Chris@4
|
111 {% elseif subfield.type == 'numeric' %}
|
Chris@4
|
112 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
113 '#type' => 'number',
|
Chris@4
|
114 {{ title }}
|
Chris@4
|
115 {{ default_value }}
|
Chris@4
|
116 '#step' => 0.01,
|
Chris@4
|
117 ];
|
Chris@4
|
118 {% elseif subfield.type == 'email' %}
|
Chris@4
|
119 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
120 '#type' => 'email',
|
Chris@4
|
121 {{ title }}
|
Chris@4
|
122 {{ default_value }}
|
Chris@4
|
123 {% if inline %}
|
Chris@4
|
124 {{ size }}
|
Chris@4
|
125 {% endif %}
|
Chris@4
|
126 ];
|
Chris@4
|
127 {% elseif subfield.type == 'telephone' %}
|
Chris@4
|
128 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
129 '#type' => 'tel',
|
Chris@4
|
130 {{ title }}
|
Chris@4
|
131 {{ default_value }}
|
Chris@4
|
132 {% if inline %}
|
Chris@4
|
133 {{ size }}
|
Chris@4
|
134 {% endif %}
|
Chris@4
|
135 ];
|
Chris@4
|
136 {% elseif subfield.type == 'uri' %}
|
Chris@4
|
137 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
138 '#type' => 'url',
|
Chris@4
|
139 {{ title }}
|
Chris@4
|
140 {{ default_value }}
|
Chris@4
|
141 {% if inline %}
|
Chris@4
|
142 {{ size }}
|
Chris@4
|
143 {% endif %}
|
Chris@4
|
144 ];
|
Chris@4
|
145 {% elseif subfield.type == 'datetime' %}
|
Chris@4
|
146 $element['{{ subfield.machine_name }}'] = [
|
Chris@4
|
147 '#type' => 'datetime',
|
Chris@4
|
148 {{ title }}
|
Chris@4
|
149 '#default_value' => NULL,
|
Chris@4
|
150 {% if subfield.date_type == 'date' %}
|
Chris@4
|
151 '#date_time_element' => 'none',
|
Chris@4
|
152 '#date_time_format' => '',
|
Chris@4
|
153 {% endif %}
|
Chris@4
|
154 ];
|
Chris@4
|
155 if (isset($items[$delta]->{{ subfield.machine_name }})) {
|
Chris@4
|
156 $element['{{ subfield.machine_name }}']['#default_value'] = DrupalDateTime::createFromFormat(
|
Chris@4
|
157 '{{ subfield.date_storage_format }}',
|
Chris@4
|
158 $items[$delta]->{{ subfield.machine_name }},
|
Chris@4
|
159 'UTC'
|
Chris@4
|
160 );
|
Chris@4
|
161 }
|
Chris@4
|
162 {% endif %}
|
Chris@4
|
163 {% endif %}
|
Chris@4
|
164
|
Chris@4
|
165 {% endfor %}
|
Chris@4
|
166 $element['#theme_wrappers'] = ['container', 'form_element'];
|
Chris@4
|
167 {% if inline %}
|
Chris@4
|
168 $element['#attributes']['class'][] = 'container-inline';
|
Chris@4
|
169 {% endif %}
|
Chris@4
|
170 $element['#attributes']['class'][] = '{{ field_id|u2h }}-elements';
|
Chris@4
|
171 $element['#attached']['library'][] = '{{ machine_name }}/{{ field_id }}';
|
Chris@4
|
172
|
Chris@4
|
173 return $element;
|
Chris@4
|
174 }
|
Chris@4
|
175
|
Chris@4
|
176 /**
|
Chris@4
|
177 * {@inheritdoc}
|
Chris@4
|
178 */
|
Chris@4
|
179 public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
|
Chris@4
|
180 return isset($violation->arrayPropertyPath[0]) ? $element[$violation->arrayPropertyPath[0]] : $element;
|
Chris@4
|
181 }
|
Chris@4
|
182
|
Chris@4
|
183 /**
|
Chris@4
|
184 * {@inheritdoc}
|
Chris@4
|
185 */
|
Chris@4
|
186 public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
|
Chris@4
|
187 foreach ($values as $delta => $value) {
|
Chris@4
|
188 {% for subfield in subfields %}
|
Chris@4
|
189 if ($value['{{ subfield.machine_name }}'] === '') {
|
Chris@4
|
190 $values[$delta]['{{ subfield.machine_name }}'] = NULL;
|
Chris@4
|
191 }
|
Chris@4
|
192 {% if subfield.type == 'datetime' %}
|
Chris@4
|
193 if ($value['{{ subfield.machine_name }}'] instanceof DrupalDateTime) {
|
Chris@4
|
194 $values[$delta]['{{ subfield.machine_name }}'] = $value['{{ subfield.machine_name }}']->format('{{ subfield.date_storage_format }}');
|
Chris@4
|
195 }
|
Chris@4
|
196 {% endif %}
|
Chris@4
|
197 {% endfor %}
|
Chris@4
|
198 }
|
Chris@4
|
199 return $values;
|
Chris@4
|
200 }
|
Chris@4
|
201
|
Chris@4
|
202 }
|