Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
CRMWebsiteWidget | |
0.00% |
0 / 35 |
|
0.00% |
0 / 6 |
110 | |
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 | |||
formElement | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
errorElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
massageFormValues | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace Drupal\crm_field\Plugin\Field\FieldWidget; |
4 | |
5 | use Drupal\Core\Field\FieldItemListInterface; |
6 | use Drupal\Core\Field\WidgetBase; |
7 | use Drupal\Core\Form\FormStateInterface; |
8 | use Drupal\crm_field\Plugin\Field\FieldType\CRMWebsiteItem; |
9 | use Symfony\Component\Validator\ConstraintViolationInterface; |
10 | |
11 | /** |
12 | * Defines the 'crm_website' field widget. |
13 | * |
14 | * @FieldWidget( |
15 | * id = "crm_website", |
16 | * label = @Translation("CRM Website"), |
17 | * field_types = {"crm_website"}, |
18 | * ) |
19 | */ |
20 | class CRMWebsiteWidget extends WidgetBase { |
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 formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
55 | |
56 | $element['url'] = [ |
57 | '#type' => 'url', |
58 | '#title' => $this->t('Url'), |
59 | '#default_value' => $items[$delta]->url ?? NULL, |
60 | '#size' => 20, |
61 | ]; |
62 | |
63 | $element['type'] = [ |
64 | '#type' => 'select', |
65 | '#title' => $this->t('Type'), |
66 | '#options' => ['' => $this->t('- None -')] + CRMWebsiteItem::allowedTypeValues(), |
67 | '#default_value' => $items[$delta]->type ?? NULL, |
68 | ]; |
69 | |
70 | $element['#theme_wrappers'] = ['container', 'form_element']; |
71 | $element['#attributes']['class'][] = 'container-inline'; |
72 | $element['#attributes']['class'][] = 'crm-website-elements'; |
73 | $element['#attached']['library'][] = 'crm_field/crm_website'; |
74 | |
75 | return $element; |
76 | } |
77 | |
78 | /** |
79 | * {@inheritdoc} |
80 | */ |
81 | public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { |
82 | return isset($violation->arrayPropertyPath[0]) ? $element[$violation->arrayPropertyPath[0]] : $element; |
83 | } |
84 | |
85 | /** |
86 | * {@inheritdoc} |
87 | */ |
88 | public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { |
89 | foreach ($values as $delta => $value) { |
90 | if ($value['url'] === '') { |
91 | $values[$delta]['url'] = NULL; |
92 | } |
93 | if ($value['type'] === '') { |
94 | $values[$delta]['type'] = NULL; |
95 | } |
96 | } |
97 | return $values; |
98 | } |
99 | |
100 | } |