diff --git a/config/schema/commerce.schema.yml b/config/schema/commerce.schema.yml index bd79eed468..b59f5542bd 100644 --- a/config/schema/commerce.schema.yml +++ b/config/schema/commerce.schema.yml @@ -53,6 +53,9 @@ field.widget.settings.commerce_entity_select: type: mapping label: 'Entity select widget settings' mapping: + hide_single_entity: + type: boolean + label: 'Hide if there''s only one entity' autocomplete_threshold: type: integer label: 'Autocomplete threshold' diff --git a/src/Element/EntitySelect.php b/src/Element/EntitySelect.php index 4140bd39f7..36b8d71c73 100644 --- a/src/Element/EntitySelect.php +++ b/src/Element/EntitySelect.php @@ -9,16 +9,18 @@ /** * Provides a form input element for selecting one or multiple entities. * + * The element is transformed based on the number of available entities: + * 1..#autocomplete_threshold: Checkboxes/radios element, based on #multiple. + * >#autocomplete_threshold: entity autocomplete element. * If the element is required, and there's only one available entity, a hidden - * form element is used. Otherwise the element is transformed based on just the - * number of available entities: - * 1..#autocomplete_threshold: Checkboxes/radios element, based on #multiple. - * >#autocomplete_treshold: entity autocomplete element. + * form element can be used instead of checkboxes/radios. * * Properties: * - #target_type: The entity type being selected. * - #multiple: Whether the user may select more than one item. * - #default_value: An entity ID or an array of entity IDs. + * - #hide_single_entity: Whether to use a hidden element when there's only one + * available entity and the element is required. * - #autocomplete_threshold: Determines when to use the autocomplete. * - #autocomplete_size: The size of the autocomplete element in characters. * - #autocomplete_placeholder: The placeholder for the autocomplete element. @@ -47,6 +49,7 @@ public function getInfo() { '#input' => TRUE, '#target_type' => '', '#multiple' => FALSE, + '#hide_single_entity' => TRUE, '#autocomplete_threshold' => 7, '#autocomplete_size' => 60, '#autocomplete_placeholder' => '', @@ -73,7 +76,7 @@ public static function processEntitySelect(&$element, FormStateInterface $form_s $entity_count = $storage->getQuery()->count()->execute(); $element['#tree'] = TRUE; // No need to show anything, there's only one possible value. - if ($element['#required'] && $entity_count == 1) { + if ($element['#required'] && $entity_count == 1 && $element['#hide_single_entity']) { $entity_ids = $storage->getQuery()->execute(); $element['value'] = [ '#type' => 'hidden', diff --git a/src/Plugin/Field/FieldWidget/EntitySelectWidget.php b/src/Plugin/Field/FieldWidget/EntitySelectWidget.php index 53eb1ed48a..193bcfdd26 100644 --- a/src/Plugin/Field/FieldWidget/EntitySelectWidget.php +++ b/src/Plugin/Field/FieldWidget/EntitySelectWidget.php @@ -25,6 +25,7 @@ class EntitySelectWidget extends WidgetBase { */ public static function defaultSettings() { return [ + 'hide_single_entity' => TRUE, 'autocomplete_threshold' => 7, 'autocomplete_size' => 60, 'autocomplete_placeholder' => '', @@ -36,6 +37,12 @@ public static function defaultSettings() { */ 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'), @@ -67,11 +74,21 @@ public function settingsForm(array $form, FormStateInterface $formState) { */ public function settingsSummary() { $summary = []; - $summary[] = $this->t('Autocomplete threshold: @threshold entities.', ['@threshold' => $this->getSetting('autocomplete_threshold')]); - $summary[] = $this->t('Autocomplete size: @size characters', ['@size' => $this->getSetting('autocomplete_size')]); + $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]); + $summary[] = $this->t('Autocomplete placeholder: @placeholder', [ + '@placeholder' => $placeholder, + ]); } else { $summary[] = $this->t('No autocomplete placeholder'); @@ -99,6 +116,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#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'],