Skip to content

Commit e290888

Browse files
committed
Issue #2897301 by bojanz: Make commerce_entity_select single-field-hiding behavior optional
1 parent 9909729 commit e290888

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

config/schema/commerce.schema.yml

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ field.widget.settings.commerce_entity_select:
5353
type: mapping
5454
label: 'Entity select widget settings'
5555
mapping:
56+
hide_single_entity:
57+
type: boolean
58+
label: 'Hide if there''s only one entity'
5659
autocomplete_threshold:
5760
type: integer
5861
label: 'Autocomplete threshold'

src/Element/EntitySelect.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
/**
1010
* Provides a form input element for selecting one or multiple entities.
1111
*
12+
* The element is transformed based on the number of available entities:
13+
* 1..#autocomplete_threshold: Checkboxes/radios element, based on #multiple.
14+
* >#autocomplete_threshold: entity autocomplete element.
1215
* If the element is required, and there's only one available entity, a hidden
13-
* form element is used. Otherwise the element is transformed based on just the
14-
* number of available entities:
15-
* 1..#autocomplete_threshold: Checkboxes/radios element, based on #multiple.
16-
* >#autocomplete_treshold: entity autocomplete element.
16+
* form element can be used instead of checkboxes/radios.
1717
*
1818
* Properties:
1919
* - #target_type: The entity type being selected.
2020
* - #multiple: Whether the user may select more than one item.
2121
* - #default_value: An entity ID or an array of entity IDs.
22+
* - #hide_single_entity: Whether to use a hidden element when there's only one
23+
* available entity and the element is required.
2224
* - #autocomplete_threshold: Determines when to use the autocomplete.
2325
* - #autocomplete_size: The size of the autocomplete element in characters.
2426
* - #autocomplete_placeholder: The placeholder for the autocomplete element.
@@ -47,6 +49,7 @@ public function getInfo() {
4749
'#input' => TRUE,
4850
'#target_type' => '',
4951
'#multiple' => FALSE,
52+
'#hide_single_entity' => TRUE,
5053
'#autocomplete_threshold' => 7,
5154
'#autocomplete_size' => 60,
5255
'#autocomplete_placeholder' => '',
@@ -73,7 +76,7 @@ public static function processEntitySelect(&$element, FormStateInterface $form_s
7376
$entity_count = $storage->getQuery()->count()->execute();
7477
$element['#tree'] = TRUE;
7578
// No need to show anything, there's only one possible value.
76-
if ($element['#required'] && $entity_count == 1) {
79+
if ($element['#required'] && $entity_count == 1 && $element['#hide_single_entity']) {
7780
$entity_ids = $storage->getQuery()->execute();
7881
$element['value'] = [
7982
'#type' => 'hidden',

src/Plugin/Field/FieldWidget/EntitySelectWidget.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)