@@ -25,6 +25,7 @@ class EntitySelectWidget extends WidgetBase {
2525 */
2626 public static function defaultSettings () {
2727 return [
28+ 'hide_single_entity ' => TRUE ,
2829 'autocomplete_threshold ' => 7 ,
2930 'autocomplete_size ' => 60 ,
3031 'autocomplete_placeholder ' => '' ,
@@ -36,6 +37,12 @@ public static function defaultSettings() {
3637 */
3738 public function settingsForm (array $ form , FormStateInterface $ formState ) {
3839 $ 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+ ];
3946 $ element ['autocomplete_threshold ' ] = [
4047 '#type ' => 'number ' ,
4148 '#title ' => $ this ->t ('Autocomplete threshold ' ),
@@ -67,11 +74,21 @@ public function settingsForm(array $form, FormStateInterface $formState) {
6774 */
6875 public function settingsSummary () {
6976 $ 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+ ]);
7287 $ placeholder = $ this ->getSetting ('autocomplete_placeholder ' );
7388 if (!empty ($ placeholder )) {
74- $ summary [] = $ this ->t ('Autocomplete placeholder: @placeholder ' , ['@placeholder ' => $ placeholder ]);
89+ $ summary [] = $ this ->t ('Autocomplete placeholder: @placeholder ' , [
90+ '@placeholder ' => $ placeholder ,
91+ ]);
7592 }
7693 else {
7794 $ summary [] = $ this ->t ('No autocomplete placeholder ' );
@@ -99,6 +116,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
99116 '#target_type ' => $ this ->getFieldSetting ('target_type ' ),
100117 '#multiple ' => $ multiple ,
101118 '#default_value ' => $ default_value ,
119+ '#hide_single_entity ' => $ settings ['hide_single_entity ' ],
102120 '#autocomplete_threshold ' => $ settings ['autocomplete_threshold ' ],
103121 '#autocomplete_size ' => $ settings ['autocomplete_size ' ],
104122 '#autocomplete_placeholder ' => $ settings ['autocomplete_placeholder ' ],
0 commit comments