diff --git a/src/Plugin/KeyInput/ApigeeAuthKeyInput.php b/src/Plugin/KeyInput/ApigeeAuthKeyInput.php index ba3e0c6b7..65ee516a2 100644 --- a/src/Plugin/KeyInput/ApigeeAuthKeyInput.php +++ b/src/Plugin/KeyInput/ApigeeAuthKeyInput.php @@ -43,26 +43,7 @@ class ApigeeAuthKeyInput extends KeyInputBase { * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - // When AJAX rebuilds the the form (f.e.: the "Send request" button) the - // submitted data is only available in $form_state->getUserInput() and not - // in $form_state->getValues(). Key is not prepared to handle this out of - // the box this is the reason why we have to manually process the user - // input and retrieve the submitted values here. - $key_value = $form_state->get('key_value')['current']; - // Either null or an empty string. - if (empty($key_value)) { - // When "Test connection" reloads the page they are not yet processed. - // @see \Drupal\key\Form\KeyFormBase::createPluginFormState() - $key_input_plugin_form_state = clone $form_state; - $key_input_plugin_form_state->setValues($form_state->getUserInput()['key_input_settings']); - // @see \Drupal\key\Form\KeyFormBase::validateForm() - $key_input_processed_values = $form_state->getFormObject()->getEntity()->getKeyInput()->processSubmittedKeyValue($key_input_plugin_form_state); - $key_value = $key_input_processed_values['processed_submitted']; - } - - // Could be an empty array. - $values = Json::decode($key_value); - $values['authorization_server_type'] = empty($values['authorization_server']) ? 'default' : 'custom'; + $values = $this->getFormDefaultValues($form_state); $state_for_public = [ ':input[name="key_input_settings[instance_type]"]' => ['value' => EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC], @@ -260,21 +241,6 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form * {@inheritdoc} */ public function processSubmittedKeyValue(FormStateInterface $form_state) { - $key_value = $form_state->get('key_value')['current']; - // Either null or an empty string. - if (empty($key_value)) { - // When "Test connection" reloads the page they are not yet processed. - // @see \Drupal\key\Form\KeyFormBase::createPluginFormState() - $key_input_plugin_form_state = clone $form_state; - $key_input_plugin_form_state->setValues($form_state->getUserInput()['key_input_settings']); - // @see \Drupal\key\Form\KeyFormBase::validateForm() - $key_input_processed_values = $form_state->getFormObject()->getEntity()->getKeyInput()->processSubmittedKeyValue($key_input_plugin_form_state); - $key_value = $key_input_processed_values['processed_submitted']; - } - - // Could be an empty array. - $values = Json::decode($key_value); - // Get input values. $input_values = $form_state->getValues(); @@ -304,7 +270,8 @@ public function processSubmittedKeyValue(FormStateInterface $form_state) { else { $input_values['account_json_key'] = ''; // If password field is empty we just skip it and preserve initial password. - if (empty($input_values['password'])) { + $values = $this->getFormDefaultValues($form_state); + if (empty($input_values['password']) && !empty($values['password'])) { $input_values['password'] = $values['password']; } } @@ -318,4 +285,31 @@ public function processSubmittedKeyValue(FormStateInterface $form_state) { return parent::processSubmittedKeyValue($form_state); } + /** + * Get authentication from values. + */ + protected function getFormDefaultValues(FormStateInterface $form_state) { + // When AJAX rebuilds the the form (f.e.: the "Send request" button) the + // submitted data is only available in $form_state->getUserInput() and not + // in $form_state->getValues(). Key is not prepared to handle this out of + // the box this is the reason why we have to manually process the user + // input and retrieve the submitted values here. + $key_value = $form_state->get('key_value')['current']; + // Either null or an empty string. + if (empty($key_value)) { + // When "Test connection" reloads the page they are not yet processed. + // @see \Drupal\key\Form\KeyFormBase::createPluginFormState() + $key_input_plugin_form_state = clone $form_state; + $key_input_plugin_form_state->setValues($form_state->getUserInput()['key_input_settings']); + // @see \Drupal\key\Form\KeyFormBase::validateForm() + $key_input_processed_values = $form_state->getFormObject()->getEntity()->getKeyInput()->processSubmittedKeyValue($key_input_plugin_form_state); + $key_value = $key_input_processed_values['processed_submitted']; + } + + // Could be an empty array. + $values = Json::decode($key_value); + $values['authorization_server_type'] = empty($values['authorization_server']) ? 'default' : 'custom'; + return $values; + } + }