From da00ab31c2e6dd4cad9b9d31a86dd167641e6a93 Mon Sep 17 00:00:00 2001 From: florenttorregrosa Date: Mon, 6 Jan 2020 11:21:59 +0100 Subject: [PATCH] Issue #3095252 by quicksketch, Grimreaper: Hide or pre-select Entity Share Channels or Sites when only one option is available --- .../entity_share_client/src/Form/PullForm.php | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/modules/entity_share_client/src/Form/PullForm.php b/modules/entity_share_client/src/Form/PullForm.php index 420609a..1e61432 100644 --- a/modules/entity_share_client/src/Form/PullForm.php +++ b/modules/entity_share_client/src/Form/PullForm.php @@ -129,16 +129,16 @@ class PullForm extends FormBase { * The module handler service. */ public function __construct( - EntityTypeManagerInterface $entity_type_manager, - EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, - RemoteManagerInterface $remote_manager, - JsonapiHelperInterface $jsonapi_helper, - RequestStack $request_stack, - LanguageManagerInterface $language_manager, - RequestServiceInterface $request_service, - RendererInterface $renderer, - ModuleHandlerInterface $module_handler - ) { + EntityTypeManagerInterface $entity_type_manager, + EntityDefinitionUpdateManagerInterface $entity_definition_update_manager, + RemoteManagerInterface $remote_manager, + JsonapiHelperInterface $jsonapi_helper, + RequestStack $request_stack, + LanguageManagerInterface $language_manager, + RequestServiceInterface $request_service, + RendererInterface $renderer, + ModuleHandlerInterface $module_handler + ) { $this->entityTypeManager = $entity_type_manager; $this->entityDefinitionUpdateManager = $entity_definition_update_manager; $this->remoteWebsites = $entity_type_manager @@ -181,14 +181,25 @@ class PullForm extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { + $remote_options = $this->prepareRemoteOptions(); + $remote_disabled = FALSE; + $remote_default_value = $this->query->get('remote'); + + // If only one option. Pre-select it and disable the select. + if (count($remote_options) == 1) { + $remote_disabled = TRUE; + $remote_default_value = array_key_first($remote_options); + $form_state->setValue('remote', $remote_default_value); + } $form['remote'] = [ '#type' => 'select', '#title' => $this->t('Remote website'), - '#options' => $this->prepareRemoteOptions(), - '#default_value' => $this->query->get('remote'), + '#options' => $remote_options, + '#default_value' => $remote_default_value, '#empty_value' => '', '#required' => TRUE, + '#disabled' => $remote_disabled, '#ajax' => [ 'callback' => [get_class($this), 'buildAjaxChannelSelect'], 'effect' => 'fade', @@ -404,13 +415,25 @@ class PullForm extends FormBase { $selected_remote = $this->remoteWebsites[$selected_remote]; $this->channelsInfos = $this->remoteManager->getChannelsInfos($selected_remote); + $channel_options = $this->getChannelOptions(); + $channel_disabled = FALSE; + $channel_default_value = $this->query->get('channel'); + + // If only one channel. Pre-select it and disable the select. + if (count($channel_options) == 1) { + $channel_disabled = TRUE; + $channel_default_value = array_key_first($channel_options); + $form_state->setValue('channel', $channel_default_value); + } + $form['channel_wrapper']['channel'] = [ '#type' => 'select', '#title' => $this->t('Channel'), - '#options' => $this->getChannelOptions(), - '#default_value' => $this->query->get('channel'), + '#options' => $channel_options, + '#default_value' => $channel_default_value, '#empty_value' => '', '#required' => TRUE, + '#disabled' => $channel_disabled, '#ajax' => [ 'callback' => [get_class($this), 'buildAjaxEntitiesSelectTable'], 'effect' => 'fade', -- 2.17.1