Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 166 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
EmailKeyValueFormatter | |
0.00% |
0 / 166 |
|
0.00% |
0 / 3 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
viewElements | |
0.00% |
0 / 154 |
|
0.00% |
0 / 1 |
156 |
1 | <?php |
2 | |
3 | namespace Drupal\crm_field\Plugin\Field\FieldFormatter; |
4 | |
5 | use Drupal\Core\Datetime\DateFormatterInterface; |
6 | use Drupal\Core\Datetime\DrupalDateTime; |
7 | use Drupal\Core\Field\FieldDefinitionInterface; |
8 | use Drupal\Core\Field\FieldItemListInterface; |
9 | use Drupal\Core\Field\FormatterBase; |
10 | use Symfony\Component\DependencyInjection\ContainerInterface; |
11 | |
12 | /** |
13 | * Plugin implementation of the 'crm_email_key_value' formatter. |
14 | * |
15 | * @FieldFormatter( |
16 | * id = "crm_email_key_value", |
17 | * label = @Translation("Key-value"), |
18 | * field_types = {"crm_email"} |
19 | * ) |
20 | */ |
21 | class EmailKeyValueFormatter extends FormatterBase { |
22 | |
23 | /** |
24 | * The date formatter service. |
25 | * |
26 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
27 | */ |
28 | protected $dateFormatter; |
29 | |
30 | /** |
31 | * Constructs a EmailKeyValueFormatter object. |
32 | * |
33 | * @param string $plugin_id |
34 | * The plugin_id for the formatter. |
35 | * @param mixed $plugin_definition |
36 | * The plugin implementation definition. |
37 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
38 | * The definition of the field to which the formatter is associated. |
39 | * @param array $settings |
40 | * The formatter settings. |
41 | * @param string $label |
42 | * The formatter label display setting. |
43 | * @param string $view_mode |
44 | * The view mode. |
45 | * @param array $third_party_settings |
46 | * Any third party settings. |
47 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
48 | * The date formatter service. |
49 | */ |
50 | public function __construct( |
51 | $plugin_id, |
52 | $plugin_definition, |
53 | FieldDefinitionInterface $field_definition, |
54 | array $settings, |
55 | $label, |
56 | $view_mode, |
57 | array $third_party_settings, |
58 | DateFormatterInterface $date_formatter, |
59 | ) { |
60 | |
61 | parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); |
62 | |
63 | $this->dateFormatter = $date_formatter; |
64 | } |
65 | |
66 | /** |
67 | * {@inheritdoc} |
68 | */ |
69 | final public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
70 | return new self( |
71 | $plugin_id, |
72 | $plugin_definition, |
73 | $configuration['field_definition'], |
74 | $configuration['settings'], |
75 | $configuration['label'], |
76 | $configuration['view_mode'], |
77 | $configuration['third_party_settings'], |
78 | $container->get('date.formatter') |
79 | ); |
80 | } |
81 | |
82 | /** |
83 | * {@inheritdoc} |
84 | */ |
85 | public function viewElements(FieldItemListInterface $items, $langcode) { |
86 | |
87 | $element = []; |
88 | |
89 | foreach ($items as $delta => $item) { |
90 | |
91 | $table = [ |
92 | '#type' => 'table', |
93 | ]; |
94 | |
95 | // Email. |
96 | if ($item->email) { |
97 | $table['#rows'][] = [ |
98 | 'data' => [ |
99 | [ |
100 | 'header' => TRUE, |
101 | 'data' => [ |
102 | '#markup' => $this->t('Email'), |
103 | ], |
104 | ], |
105 | [ |
106 | 'data' => [ |
107 | '#markup' => $item->email, |
108 | ], |
109 | ], |
110 | ], |
111 | 'no_striping' => TRUE, |
112 | ]; |
113 | } |
114 | |
115 | // Location. |
116 | if ($item->location_id) { |
117 | $table['#rows'][] = [ |
118 | 'data' => [ |
119 | [ |
120 | 'header' => TRUE, |
121 | 'data' => [ |
122 | '#markup' => $this->t('Location'), |
123 | ], |
124 | ], |
125 | [ |
126 | 'data' => [ |
127 | '#markup' => $item->location_id, |
128 | ], |
129 | ], |
130 | ], |
131 | 'no_striping' => TRUE, |
132 | ]; |
133 | } |
134 | |
135 | // Primary. |
136 | if ($item->primary) { |
137 | $table['#rows'][] = [ |
138 | 'data' => [ |
139 | [ |
140 | 'header' => TRUE, |
141 | 'data' => [ |
142 | '#markup' => $this->t('Primary'), |
143 | ], |
144 | ], |
145 | [ |
146 | 'data' => [ |
147 | '#markup' => $item->primary ? $this->t('Yes') : $this->t('No'), |
148 | ], |
149 | ], |
150 | ], |
151 | 'no_striping' => TRUE, |
152 | ]; |
153 | } |
154 | |
155 | // Hold. |
156 | if ($item->hold) { |
157 | $table['#rows'][] = [ |
158 | 'data' => [ |
159 | [ |
160 | 'header' => TRUE, |
161 | 'data' => [ |
162 | '#markup' => $this->t('Hold'), |
163 | ], |
164 | ], |
165 | [ |
166 | 'data' => [ |
167 | '#markup' => $item->hold ? $this->t('Yes') : $this->t('No'), |
168 | ], |
169 | ], |
170 | ], |
171 | 'no_striping' => TRUE, |
172 | ]; |
173 | } |
174 | |
175 | // Bulk. |
176 | if ($item->bulk) { |
177 | $table['#rows'][] = [ |
178 | 'data' => [ |
179 | [ |
180 | 'header' => TRUE, |
181 | 'data' => [ |
182 | '#markup' => $this->t('Bulk'), |
183 | ], |
184 | ], |
185 | [ |
186 | 'data' => [ |
187 | '#markup' => $item->bulk ? $this->t('Yes') : $this->t('No'), |
188 | ], |
189 | ], |
190 | ], |
191 | 'no_striping' => TRUE, |
192 | ]; |
193 | } |
194 | |
195 | // Hold Date. |
196 | if ($item->hold_date) { |
197 | $date = DrupalDateTime::createFromFormat('Y-m-d\TH:i:s', $item->hold_date); |
198 | $timestamp = $date->getTimestamp(); |
199 | $formatted_date = $this->dateFormatter->format($timestamp, 'long'); |
200 | $iso_date = $this->dateFormatter->format($timestamp, 'custom', 'Y-m-d\TH:i:s') . 'Z'; |
201 | |
202 | $table['#rows'][] = [ |
203 | 'data' => [ |
204 | [ |
205 | 'header' => TRUE, |
206 | 'data' => [ |
207 | '#markup' => $this->t('Hold Date'), |
208 | ], |
209 | ], |
210 | [ |
211 | 'data' => [ |
212 | '#theme' => 'time', |
213 | '#text' => $formatted_date, |
214 | '#html' => FALSE, |
215 | '#attributes' => [ |
216 | 'datetime' => $iso_date, |
217 | ], |
218 | '#cache' => [ |
219 | 'contexts' => [ |
220 | 'timezone', |
221 | ], |
222 | ], |
223 | ], |
224 | ], |
225 | ], |
226 | 'no_striping' => TRUE, |
227 | ]; |
228 | } |
229 | |
230 | // Reset Date. |
231 | if ($item->reset_date) { |
232 | $date = DrupalDateTime::createFromFormat('Y-m-d\TH:i:s', $item->reset_date); |
233 | $timestamp = $date->getTimestamp(); |
234 | $formatted_date = $this->dateFormatter->format($timestamp, 'long'); |
235 | $iso_date = $this->dateFormatter->format($timestamp, 'custom', 'Y-m-d\TH:i:s') . 'Z'; |
236 | |
237 | $table['#rows'][] = [ |
238 | 'data' => [ |
239 | [ |
240 | 'header' => TRUE, |
241 | 'data' => [ |
242 | '#markup' => $this->t('Reset Date'), |
243 | ], |
244 | ], |
245 | [ |
246 | 'data' => [ |
247 | '#theme' => 'time', |
248 | '#text' => $formatted_date, |
249 | '#html' => FALSE, |
250 | '#attributes' => [ |
251 | 'datetime' => $iso_date, |
252 | ], |
253 | '#cache' => [ |
254 | 'contexts' => [ |
255 | 'timezone', |
256 | ], |
257 | ], |
258 | ], |
259 | ], |
260 | ], |
261 | 'no_striping' => TRUE, |
262 | ]; |
263 | } |
264 | |
265 | $element[$delta] = $table; |
266 | |
267 | } |
268 | |
269 | return $element; |
270 | } |
271 | |
272 | } |