@@ -25,6 +25,7 @@ class EntitySelectWidget extends WidgetBase {
25
25
*/
26
26
public static function defaultSettings () {
27
27
return [
28
+ 'hide_single_entity ' => TRUE ,
28
29
'autocomplete_threshold ' => 7 ,
29
30
'autocomplete_size ' => 60 ,
30
31
'autocomplete_placeholder ' => '' ,
@@ -36,6 +37,12 @@ public static function defaultSettings() {
36
37
*/
37
38
public function settingsForm (array $ form , FormStateInterface $ formState ) {
38
39
$ element = [];
40
+ $ element ['hide_single_entity ' ] = [
41
+ '#type ' => 'checkbox ' ,
42
+ '#title ' => t ("Hide if there's only one available entity. " ),
43
+ '#default_value ' => $ this ->getSetting ('hide_single_entity ' ),
44
+ '#access ' => $ this ->fieldDefinition ->isRequired (),
45
+ ];
39
46
$ element ['autocomplete_threshold ' ] = [
40
47
'#type ' => 'number ' ,
41
48
'#title ' => $ this ->t ('Autocomplete threshold ' ),
@@ -67,11 +74,21 @@ public function settingsForm(array $form, FormStateInterface $formState) {
67
74
*/
68
75
public function settingsSummary () {
69
76
$ summary = [];
70
- $ summary [] = $ this ->t ('Autocomplete threshold: @threshold entities. ' , ['@threshold ' => $ this ->getSetting ('autocomplete_threshold ' )]);
71
- $ summary [] = $ this ->t ('Autocomplete size: @size characters ' , ['@size ' => $ this ->getSetting ('autocomplete_size ' )]);
77
+ $ hide_single_entity = $ this ->getSetting ('hide_single_entity ' );
78
+ if ($ this ->fieldDefinition ->isRequired () && $ hide_single_entity ) {
79
+ $ summary [] = $ this ->t ("Hide if there's only one available entity " );
80
+ }
81
+ $ summary [] = $ this ->t ('Autocomplete threshold: @threshold entities. ' , [
82
+ '@threshold ' => $ this ->getSetting ('autocomplete_threshold ' ),
83
+ ]);
84
+ $ summary [] = $ this ->t ('Autocomplete size: @size characters ' , [
85
+ '@size ' => $ this ->getSetting ('autocomplete_size ' ),
86
+ ]);
72
87
$ placeholder = $ this ->getSetting ('autocomplete_placeholder ' );
73
88
if (!empty ($ placeholder )) {
74
- $ summary [] = $ this ->t ('Autocomplete placeholder: @placeholder ' , ['@placeholder ' => $ placeholder ]);
89
+ $ summary [] = $ this ->t ('Autocomplete placeholder: @placeholder ' , [
90
+ '@placeholder ' => $ placeholder ,
91
+ ]);
75
92
}
76
93
else {
77
94
$ summary [] = $ this ->t ('No autocomplete placeholder ' );
@@ -99,6 +116,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
99
116
'#target_type ' => $ this ->getFieldSetting ('target_type ' ),
100
117
'#multiple ' => $ multiple ,
101
118
'#default_value ' => $ default_value ,
119
+ '#hide_single_entity ' => $ settings ['hide_single_entity ' ],
102
120
'#autocomplete_threshold ' => $ settings ['autocomplete_threshold ' ],
103
121
'#autocomplete_size ' => $ settings ['autocomplete_size ' ],
104
122
'#autocomplete_placeholder ' => $ settings ['autocomplete_placeholder ' ],
0 commit comments