Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CRMTelephoneDefaultFormatter | |
0.00% |
0 / 55 |
|
0.00% |
0 / 4 |
132 | |
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 | |||
viewElements | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
72 |
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\Core\Form\FormStateInterface; |
8 | use Drupal\Core\Url; |
9 | use Drupal\crm_field\Plugin\Field\FieldType\CRMTelephoneItem; |
10 | |
11 | /** |
12 | * Plugin implementation of the 'crm_telephone_default' formatter. |
13 | * |
14 | * @FieldFormatter( |
15 | * id = "crm_telephone_default", |
16 | * label = @Translation("Default"), |
17 | * field_types = {"crm_telephone"} |
18 | * ) |
19 | */ |
20 | class CRMTelephoneDefaultFormatter extends FormatterBase { |
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 viewElements(FieldItemListInterface $items, $langcode) { |
55 | $element = []; |
56 | |
57 | foreach ($items as $delta => $item) { |
58 | |
59 | if ($item->phone) { |
60 | $element[$delta]['phone'] = [ |
61 | '#type' => 'item', |
62 | '#title' => $this->t('Phone'), |
63 | 'content' => [ |
64 | '#type' => 'link', |
65 | '#title' => $item->phone, |
66 | '#url' => Url::fromUri('tel:' . rawurlencode(preg_replace('/\s+/', '', $item->phone))), |
67 | ], |
68 | ]; |
69 | } |
70 | |
71 | if ($item->phone_ext) { |
72 | $element[$delta]['phone_ext'] = [ |
73 | '#type' => 'item', |
74 | '#title' => $this->t('Extension'), |
75 | '#markup' => $item->phone_ext, |
76 | ]; |
77 | } |
78 | |
79 | if ($item->type) { |
80 | $allowed_values = CRMTelephoneItem::allowedTypeValues(); |
81 | $element[$delta]['type'] = [ |
82 | '#type' => 'item', |
83 | '#title' => $this->t('Type'), |
84 | '#markup' => $allowed_values[$item->type], |
85 | ]; |
86 | } |
87 | |
88 | if ($item->location_id) { |
89 | $element[$delta]['location_id'] = [ |
90 | '#type' => 'item', |
91 | '#title' => $this->t('Location'), |
92 | '#markup' => $item->location_id, |
93 | ]; |
94 | } |
95 | |
96 | $element[$delta]['primary'] = [ |
97 | '#type' => 'item', |
98 | '#title' => $this->t('Primary'), |
99 | '#markup' => $item->primary ? $this->t('Yes') : $this->t('No'), |
100 | ]; |
101 | |
102 | if ($item->mobile_provider_id) { |
103 | $allowed_values = CRMTelephoneItem::allowedMobileProviderValues(); |
104 | $element[$delta]['mobile_provider_id'] = [ |
105 | '#type' => 'item', |
106 | '#title' => $this->t('Mobile Provider'), |
107 | '#markup' => $allowed_values[$item->mobile_provider_id], |
108 | ]; |
109 | } |
110 | |
111 | } |
112 | |
113 | return $element; |
114 | } |
115 | |
116 | } |