Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 110 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
EmailWidget | |
0.00% |
0 / 110 |
|
0.00% |
0 / 7 |
650 | |
0.00% |
0 / 1 |
defaultSettings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
settingsForm | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
settingsSummary | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
formElement | |
0.00% |
0 / 63 |
|
0.00% |
0 / 1 |
20 | |||
getLocations | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
errorElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
massageFormValues | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
240 |
1 | <?php |
2 | |
3 | namespace Drupal\crm_field\Plugin\Field\FieldWidget; |
4 | |
5 | use Drupal\Core\Datetime\DrupalDateTime; |
6 | use Drupal\Core\Field\FieldItemListInterface; |
7 | use Drupal\Core\Field\WidgetBase; |
8 | use Drupal\Core\Form\FormStateInterface; |
9 | use Symfony\Component\Validator\ConstraintViolationInterface; |
10 | |
11 | /** |
12 | * Defines the 'crm_email' field widget. |
13 | * |
14 | * @FieldWidget( |
15 | * id = "crm_email", |
16 | * label = @Translation("CRM Email"), |
17 | * field_types = {"crm_email"}, |
18 | * ) |
19 | */ |
20 | class EmailWidget extends WidgetBase { |
21 | |
22 | /** |
23 | * {@inheritdoc} |
24 | */ |
25 | public static function defaultSettings() { |
26 | return ['foo' => 'bar'] + parent::defaultSettings(); |
27 | } |
28 | |
29 | /** |
30 | * {@inheritdoc} |
31 | */ |
32 | public function settingsForm(array $form, FormStateInterface $form_state) { |
33 | $settings = $this->getSettings(); |
34 | $element['foo'] = [ |
35 | '#type' => 'textfield', |
36 | '#title' => $this->t('Foo'), |
37 | '#default_value' => $settings['foo'], |
38 | ]; |
39 | return $element; |
40 | } |
41 | |
42 | /** |
43 | * {@inheritdoc} |
44 | */ |
45 | public function settingsSummary() { |
46 | $settings = $this->getSettings(); |
47 | $summary[] = $this->t('Foo: @foo', ['@foo' => $settings['foo']]); |
48 | return $summary; |
49 | } |
50 | |
51 | /** |
52 | * {@inheritdoc} |
53 | */ |
54 | public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
55 | $field_name = $this->fieldDefinition->getName(); |
56 | $element['primary'] = [ |
57 | '#type' => 'radio', |
58 | '#return_value' => $delta, |
59 | '#title' => $this->t('Primary'), |
60 | '#default_value' => $items[$delta]->primary ? $delta : NULL, |
61 | '#attributes' => [ |
62 | 'id' => $field_name . '-primary-' . $delta, |
63 | ], |
64 | '#parents' => [$field_name, 0, 'primary'], |
65 | ]; |
66 | |
67 | $element['email'] = [ |
68 | '#type' => 'email', |
69 | '#title' => $this->t('Email'), |
70 | '#default_value' => $items[$delta]->email ?? NULL, |
71 | ]; |
72 | |
73 | $element['location_id'] = [ |
74 | '#type' => 'select', |
75 | '#title' => $this->t('Location'), |
76 | '#default_value' => $items[$delta]->location_id ?? NULL, |
77 | '#options' => $this->getLocations(), |
78 | ]; |
79 | |
80 | $element['hold'] = [ |
81 | '#type' => 'checkbox', |
82 | '#title' => $this->t('Hold'), |
83 | '#default_value' => $items[$delta]->hold ?? NULL, |
84 | ]; |
85 | |
86 | $element['bulk'] = [ |
87 | '#type' => 'checkbox', |
88 | '#title' => $this->t('Bulk'), |
89 | '#default_value' => $items[$delta]->bulk ?? NULL, |
90 | ]; |
91 | |
92 | $element['hold_date'] = [ |
93 | '#type' => 'datetime', |
94 | '#title' => $this->t('Hold Date'), |
95 | '#default_value' => NULL, |
96 | '#state' => [ |
97 | 'visible' => [ |
98 | ':input[name="field_email[' . $delta . '][hold]"]' => ['checked' => TRUE], |
99 | ], |
100 | ], |
101 | ]; |
102 | if (isset($items[$delta]->hold_date)) { |
103 | $element['hold_date']['#default_value'] = DrupalDateTime::createFromFormat( |
104 | 'Y-m-d\TH:i:s', |
105 | $items[$delta]->hold_date, |
106 | 'UTC' |
107 | ); |
108 | } |
109 | |
110 | $element['reset_date'] = [ |
111 | '#type' => 'datetime', |
112 | '#title' => $this->t('Reset Date'), |
113 | '#default_value' => NULL, |
114 | ]; |
115 | if (isset($items[$delta]->reset_date)) { |
116 | $element['reset_date']['#default_value'] = DrupalDateTime::createFromFormat( |
117 | 'Y-m-d\TH:i:s', |
118 | $items[$delta]->reset_date, |
119 | 'UTC' |
120 | ); |
121 | } |
122 | |
123 | $element['#theme_wrappers'] = ['container', 'form_element']; |
124 | $element['#attributes']['class'][] = 'crm-email-elements'; |
125 | $element['#attached']['library'][] = 'crm_field/crm_email'; |
126 | |
127 | return $element; |
128 | } |
129 | |
130 | /** |
131 | * Get locations. |
132 | * |
133 | * @return array |
134 | * Locations. |
135 | */ |
136 | protected function getLocations() { |
137 | $locations = []; |
138 | $locations['home'] = $this->t('Home'); |
139 | $locations['work'] = $this->t('Work'); |
140 | $locations['other'] = $this->t('Other'); |
141 | |
142 | return $locations; |
143 | } |
144 | |
145 | /** |
146 | * {@inheritdoc} |
147 | */ |
148 | public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { |
149 | return isset($violation->arrayPropertyPath[0]) ? $element[$violation->arrayPropertyPath[0]] : $element; |
150 | } |
151 | |
152 | /** |
153 | * {@inheritdoc} |
154 | */ |
155 | public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
156 | $primary = 0; |
157 | foreach ($values as $delta => $value) { |
158 | if (isset($value['primary'])) { |
159 | $primary = $value['primary']; |
160 | } |
161 | } |
162 | foreach ($values as $delta => $value) { |
163 | $values[$delta]['primary'] = FALSE; |
164 | if ($values[$delta]['_original_delta'] == $primary) { |
165 | $values[$delta]['primary'] = TRUE; |
166 | } |
167 | |
168 | if ($value['email'] === '') { |
169 | $values[$delta]['email'] = NULL; |
170 | } |
171 | if ($value['location_id'] === '') { |
172 | $values[$delta]['location_id'] = NULL; |
173 | } |
174 | if ($value['hold'] === '') { |
175 | $values[$delta]['hold'] = NULL; |
176 | } |
177 | if ($value['bulk'] === '') { |
178 | $values[$delta]['bulk'] = NULL; |
179 | } |
180 | if ($value['hold_date'] === '') { |
181 | $values[$delta]['hold_date'] = NULL; |
182 | } |
183 | if ($value['hold_date'] instanceof DrupalDateTime) { |
184 | $values[$delta]['hold_date'] = $value['hold_date']->format('Y-m-d\TH:i:s'); |
185 | } |
186 | if ($value['reset_date'] === '') { |
187 | $values[$delta]['reset_date'] = NULL; |
188 | } |
189 | if ($value['reset_date'] instanceof DrupalDateTime) { |
190 | $values[$delta]['reset_date'] = $value['reset_date']->format('Y-m-d\TH:i:s'); |
191 | } |
192 | } |
193 | |
194 | usort($values, function ($a, $b) { |
195 | if ($a['primary'] == $b['primary']) { |
196 | return 0; |
197 | } |
198 | return ($a['primary'] < $b['primary']) ? 1 : -1; |
199 | }); |
200 | |
201 | return $values; |
202 | } |
203 | |
204 | } |