Skip to content

Commit

Permalink
Issue #2897301 by bojanz: Make commerce_entity_select single-field-hi…
Browse files Browse the repository at this point in the history
…ding behavior optional
  • Loading branch information
bojanz committed Jul 27, 2017
1 parent 9909729 commit dc443c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config/schema/commerce.schema.yml
Expand Up @@ -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'
Expand Down
13 changes: 8 additions & 5 deletions src/Element/EntitySelect.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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' => '',
Expand All @@ -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',
Expand Down
24 changes: 21 additions & 3 deletions src/Plugin/Field/FieldWidget/EntitySelectWidget.php
Expand Up @@ -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' => '',
Expand All @@ -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'),
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit dc443c7

Please sign in to comment.