Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CRMWebsiteDefaultFormatter | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
56 | |
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 / 20 |
|
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\Core\Form\FormStateInterface; |
8 | use Drupal\Core\Url; |
9 | use Drupal\crm_field\Plugin\Field\FieldType\CRMWebsiteItem; |
10 | |
11 | /** |
12 | * Plugin implementation of the 'crm_website_default' formatter. |
13 | * |
14 | * @FieldFormatter( |
15 | * id = "crm_website_default", |
16 | * label = @Translation("Default"), |
17 | * field_types = {"crm_website"} |
18 | * ) |
19 | */ |
20 | class CRMWebsiteDefaultFormatter 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->url) { |
60 | $element[$delta]['url'] = [ |
61 | '#type' => 'item', |
62 | '#title' => $this->t('Url'), |
63 | 'content' => [ |
64 | '#type' => 'link', |
65 | '#title' => $item->url, |
66 | '#url' => Url::fromUri($item->url), |
67 | ], |
68 | ]; |
69 | } |
70 | |
71 | if ($item->type) { |
72 | $allowed_values = CRMWebsiteItem::allowedTypeValues(); |
73 | $element[$delta]['type'] = [ |
74 | '#type' => 'item', |
75 | '#title' => $this->t('Type'), |
76 | '#markup' => $allowed_values[$item->type], |
77 | ]; |
78 | } |
79 | |
80 | } |
81 | |
82 | return $element; |
83 | } |
84 | |
85 | } |