Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
CRMWebsiteKeyValueFormatter | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 1 |
viewElements | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
20 |
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\CRMWebsiteItem; |
8 | |
9 | /** |
10 | * Plugin implementation of the 'crm_website_key_value' formatter. |
11 | * |
12 | * @FieldFormatter( |
13 | * id = "crm_website_key_value", |
14 | * label = @Translation("Key-value"), |
15 | * field_types = {"crm_website"} |
16 | * ) |
17 | */ |
18 | class CRMWebsiteKeyValueFormatter 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 | // Url. |
34 | if ($item->url) { |
35 | $table['#rows'][] = [ |
36 | 'data' => [ |
37 | [ |
38 | 'header' => TRUE, |
39 | 'data' => [ |
40 | '#markup' => $this->t('Url'), |
41 | ], |
42 | ], |
43 | [ |
44 | 'data' => [ |
45 | '#markup' => $item->url, |
46 | ], |
47 | ], |
48 | ], |
49 | 'no_striping' => TRUE, |
50 | ]; |
51 | } |
52 | |
53 | // Type. |
54 | if ($item->type) { |
55 | $allowed_values = CRMWebsiteItem::allowedTypeValues(); |
56 | |
57 | $table['#rows'][] = [ |
58 | 'data' => [ |
59 | [ |
60 | 'header' => TRUE, |
61 | 'data' => [ |
62 | '#markup' => $this->t('Type'), |
63 | ], |
64 | ], |
65 | [ |
66 | 'data' => [ |
67 | '#markup' => $allowed_values[$item->type], |
68 | ], |
69 | ], |
70 | ], |
71 | 'no_striping' => TRUE, |
72 | ]; |
73 | } |
74 | |
75 | $element[$delta] = $table; |
76 | |
77 | } |
78 | |
79 | return $element; |
80 | } |
81 | |
82 | } |