-
Notifications
You must be signed in to change notification settings - Fork 257
/
EntitySelectWidget.php
136 lines (123 loc) · 4.32 KB
/
EntitySelectWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Drupal\commerce\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'commerce_entity_select' widget.
*
* @FieldWidget(
* id = "commerce_entity_select",
* label = @Translation("Entity select"),
* field_types = {
* "entity_reference"
* },
* multiple_values = TRUE
* )
*/
class EntitySelectWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'hide_single_entity' => TRUE,
'autocomplete_threshold' => 7,
'autocomplete_size' => 60,
'autocomplete_placeholder' => '',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $formState) {
$element = [];
$element['hide_single_entity'] = [
'#type' => 'checkbox',
'#title' => t("Hide if there's only one available entity."),
'#default_value' => $this->getSetting('hide_single_entity'),
'#access' => $this->fieldDefinition->isRequired(),
];
$element['autocomplete_threshold'] = [
'#type' => 'number',
'#title' => $this->t('Autocomplete threshold'),
'#description' => $this->t('Number of available entities after which the autocomplete is used.'),
'#default_value' => $this->getSetting('autocomplete_threshold'),
'#min' => 2,
'#required' => TRUE,
];
$element['autocomplete_size'] = [
'#type' => 'number',
'#title' => $this->t('Autocomplete size'),
'#description' => $this->t('Size of the input field in characters.'),
'#default_value' => $this->getSetting('autocomplete_size'),
'#min' => 1,
'#required' => TRUE,
];
$element['autocomplete_placeholder'] = [
'#type' => 'textfield',
'#title' => $this->t('Autocomplete placeholder'),
'#default_value' => $this->getSetting('autocomplete_placeholder'),
'#description' => $this->t('Text that will be shown inside the autocomplete field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$hide_single_entity = $this->getSetting('hide_single_entity');
if ($this->fieldDefinition->isRequired() && $hide_single_entity) {
$summary[] = $this->t("Hide if there's only one available entity");
}
$summary[] = $this->t('Autocomplete threshold: @threshold entities.', [
'@threshold' => $this->getSetting('autocomplete_threshold'),
]);
$summary[] = $this->t('Autocomplete size: @size characters', [
'@size' => $this->getSetting('autocomplete_size'),
]);
$placeholder = $this->getSetting('autocomplete_placeholder');
if (!empty($placeholder)) {
$summary[] = $this->t('Autocomplete placeholder: @placeholder', [
'@placeholder' => $placeholder,
]);
}
else {
$summary[] = $this->t('No autocomplete placeholder');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$settings = $this->getSettings();
$multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple();
$values = $items->getValue();
if ($multiple) {
$default_value = array_column($values, 'target_id');
}
else {
$default_value = !empty($values) ? $values[0]['target_id'] : NULL;
}
$element += [
'#type' => 'commerce_entity_select',
'#target_type' => $this->getFieldSetting('target_type'),
'#multiple' => $multiple,
'#default_value' => $default_value,
'#hide_single_entity' => $settings['hide_single_entity'],
'#autocomplete_threshold' => $settings['autocomplete_threshold'],
'#autocomplete_size' => $settings['autocomplete_size'],
'#autocomplete_placeholder' => $settings['autocomplete_placeholder'],
'#required' => $this->fieldDefinition->isRequired(),
];
return ['target_id' => $element];
}
/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
return $values['target_id'];
}
}