Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 90 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
CRMTelephoneWidget | |
0.00% |
0 / 90 |
|
0.00% |
0 / 6 |
380 | |
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 / 54 |
|
0.00% |
0 / 1 |
6 | |||
errorElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
massageFormValues | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
156 |
1 | <?php |
2 | |
3 | namespace Drupal\crm_field\Plugin\Field\FieldWidget; |
4 | |
5 | use Drupal\Core\Field\FieldItemListInterface; |
6 | use Drupal\Core\Field\WidgetBase; |
7 | use Drupal\Core\Form\FormStateInterface; |
8 | use Drupal\crm_field\Plugin\Field\FieldType\CRMTelephoneItem; |
9 | use Symfony\Component\Validator\ConstraintViolationInterface; |
10 | |
11 | /** |
12 | * Defines the 'crm_telephone' field widget. |
13 | * |
14 | * @FieldWidget( |
15 | * id = "crm_telephone", |
16 | * label = @Translation("CRM Telephone"), |
17 | * field_types = {"crm_telephone"}, |
18 | * ) |
19 | */ |
20 | class CRMTelephoneWidget 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['location_id'] = [ |
68 | '#type' => 'select', |
69 | '#title' => $this->t('Location'), |
70 | '#options' => CRMTelephoneItem::allowedLocationValues(), |
71 | '#default_value' => $items[$delta]->location_id ?? 'main', |
72 | '#required' => TRUE, |
73 | ]; |
74 | |
75 | $element['phone'] = [ |
76 | '#type' => 'tel', |
77 | '#title' => $this->t('Phone'), |
78 | '#default_value' => $items[$delta]->phone ?? NULL, |
79 | '#attributes' => [ |
80 | 'size' => 10, |
81 | ], |
82 | ]; |
83 | |
84 | $element['phone_ext'] = [ |
85 | '#type' => 'textfield', |
86 | '#title' => $this->t('Extension'), |
87 | '#size' => 5, |
88 | '#default_value' => $items[$delta]->phone_ext ?? NULL, |
89 | ]; |
90 | |
91 | $element['type'] = [ |
92 | '#type' => 'select', |
93 | '#title' => $this->t('Type'), |
94 | '#options' => CRMTelephoneItem::allowedTypeValues(), |
95 | '#default_value' => $items[$delta]->type ?? 'phone', |
96 | '#required' => TRUE, |
97 | ]; |
98 | |
99 | $element['mobile_provider_id'] = [ |
100 | '#type' => 'select', |
101 | '#title' => $this->t('Mobile Provider'), |
102 | '#options' => ['' => $this->t('- None -')] + CRMTelephoneItem::allowedMobileProviderValues(), |
103 | '#default_value' => $items[$delta]->mobile_provider_id ?? NULL, |
104 | '#states' => [ |
105 | 'visible' => [ |
106 | ':input[name="' . $field_name . '[' . $delta . '][type]"]' => ['value' => 'mobile'], |
107 | ], |
108 | ], |
109 | ]; |
110 | |
111 | $element['#theme_wrappers'] = ['container', 'form_element']; |
112 | $element['#attributes']['class'][] = 'crm-telephone-elements'; |
113 | $element['#attached']['library'][] = 'crm_field/crm_telephone'; |
114 | |
115 | return $element; |
116 | } |
117 | |
118 | /** |
119 | * {@inheritdoc} |
120 | */ |
121 | public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { |
122 | return isset($violation->arrayPropertyPath[0]) ? $element[$violation->arrayPropertyPath[0]] : $element; |
123 | } |
124 | |
125 | /** |
126 | * {@inheritdoc} |
127 | */ |
128 | public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
129 | $primary = 0; |
130 | foreach ($values as $delta => $value) { |
131 | if (isset($value['primary'])) { |
132 | $primary = $value['primary']; |
133 | } |
134 | } |
135 | foreach ($values as $delta => $value) { |
136 | $values[$delta]['primary'] = FALSE; |
137 | if ($values[$delta]['_original_delta'] == $primary) { |
138 | $values[$delta]['primary'] = TRUE; |
139 | } |
140 | |
141 | if ($value['phone'] === '') { |
142 | $values[$delta]['phone'] = NULL; |
143 | } |
144 | if ($value['phone_ext'] === '') { |
145 | $values[$delta]['phone_ext'] = NULL; |
146 | } |
147 | if ($value['type'] === '') { |
148 | $values[$delta]['type'] = NULL; |
149 | } |
150 | if ($value['location_id'] === '') { |
151 | $values[$delta]['location_id'] = NULL; |
152 | } |
153 | if ($value['mobile_provider_id'] === '') { |
154 | $values[$delta]['mobile_provider_id'] = NULL; |
155 | } |
156 | } |
157 | |
158 | usort($values, function ($a, $b) { |
159 | if ($a['primary'] == $b['primary']) { |
160 | return 0; |
161 | } |
162 | return ($a['primary'] < $b['primary']) ? 1 : -1; |
163 | }); |
164 | |
165 | return $values; |
166 | } |
167 | |
168 | } |