Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 111 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
CRMTelephoneKeyValueFormatter | |
0.00% |
0 / 111 |
|
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 1 |
viewElements | |
0.00% |
0 / 111 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | |
3 | namespace Drupal\crm_field\Plugin\Field\FieldFormatter; |
4 | |
5 | use Drupal\Core\Field\FieldItemListInterface; |
6 | use Drupal\Core\Field\FormatterBase; |
7 | use Drupal\crm_field\Plugin\Field\FieldType\CRMTelephoneItem; |
8 | |
9 | /** |
10 | * Plugin implementation of the 'crm_telephone_key_value' formatter. |
11 | * |
12 | * @FieldFormatter( |
13 | * id = "crm_telephone_key_value", |
14 | * label = @Translation("Key-value"), |
15 | * field_types = {"crm_telephone"} |
16 | * ) |
17 | */ |
18 | class CRMTelephoneKeyValueFormatter extends FormatterBase { |
19 | |
20 | /** |
21 | * {@inheritdoc} |
22 | */ |
23 | public function viewElements(FieldItemListInterface $items, $langcode) { |
24 | |
25 | $element = []; |
26 | |
27 | foreach ($items as $delta => $item) { |
28 | |
29 | $table = [ |
30 | '#type' => 'table', |
31 | ]; |
32 | |
33 | // Phone. |
34 | if ($item->phone) { |
35 | $table['#rows'][] = [ |
36 | 'data' => [ |
37 | [ |
38 | 'header' => TRUE, |
39 | 'data' => [ |
40 | '#markup' => $this->t('Phone'), |
41 | ], |
42 | ], |
43 | [ |
44 | 'data' => [ |
45 | '#markup' => $item->phone, |
46 | ], |
47 | ], |
48 | ], |
49 | 'no_striping' => TRUE, |
50 | ]; |
51 | } |
52 | |
53 | // Extension. |
54 | if ($item->phone_ext) { |
55 | $table['#rows'][] = [ |
56 | 'data' => [ |
57 | [ |
58 | 'header' => TRUE, |
59 | 'data' => [ |
60 | '#markup' => $this->t('Extension'), |
61 | ], |
62 | ], |
63 | [ |
64 | 'data' => [ |
65 | '#markup' => $item->phone_ext, |
66 | ], |
67 | ], |
68 | ], |
69 | 'no_striping' => TRUE, |
70 | ]; |
71 | } |
72 | |
73 | // Type. |
74 | if ($item->type) { |
75 | $allowed_values = CRMTelephoneItem::allowedTypeValues(); |
76 | |
77 | $table['#rows'][] = [ |
78 | 'data' => [ |
79 | [ |
80 | 'header' => TRUE, |
81 | 'data' => [ |
82 | '#markup' => $this->t('Type'), |
83 | ], |
84 | ], |
85 | [ |
86 | 'data' => [ |
87 | '#markup' => $allowed_values[$item->type], |
88 | ], |
89 | ], |
90 | ], |
91 | 'no_striping' => TRUE, |
92 | ]; |
93 | } |
94 | |
95 | // Location. |
96 | if ($item->location_id) { |
97 | $table['#rows'][] = [ |
98 | 'data' => [ |
99 | [ |
100 | 'header' => TRUE, |
101 | 'data' => [ |
102 | '#markup' => $this->t('Location'), |
103 | ], |
104 | ], |
105 | [ |
106 | 'data' => [ |
107 | '#markup' => $item->location_id, |
108 | ], |
109 | ], |
110 | ], |
111 | 'no_striping' => TRUE, |
112 | ]; |
113 | } |
114 | |
115 | // Primary. |
116 | if ($item->primary) { |
117 | $table['#rows'][] = [ |
118 | 'data' => [ |
119 | [ |
120 | 'header' => TRUE, |
121 | 'data' => [ |
122 | '#markup' => $this->t('Primary'), |
123 | ], |
124 | ], |
125 | [ |
126 | 'data' => [ |
127 | '#markup' => $item->primary ? $this->t('Yes') : $this->t('No'), |
128 | ], |
129 | ], |
130 | ], |
131 | 'no_striping' => TRUE, |
132 | ]; |
133 | } |
134 | |
135 | // Mobile Provider. |
136 | if ($item->mobile_provider_id) { |
137 | $allowed_values = CRMTelephoneItem::allowedMobileProviderValues(); |
138 | |
139 | $table['#rows'][] = [ |
140 | 'data' => [ |
141 | [ |
142 | 'header' => TRUE, |
143 | 'data' => [ |
144 | '#markup' => $this->t('Mobile Provider'), |
145 | ], |
146 | ], |
147 | [ |
148 | 'data' => [ |
149 | '#markup' => $allowed_values[$item->mobile_provider_id], |
150 | ], |
151 | ], |
152 | ], |
153 | 'no_striping' => TRUE, |
154 | ]; |
155 | } |
156 | |
157 | $element[$delta] = $table; |
158 | |
159 | } |
160 | |
161 | return $element; |
162 | } |
163 | |
164 | } |