From 12c17940131c83b301fe3b93baad5aea5a271f31 Mon Sep 17 00:00:00 2001 From: Marcus Johansson Date: Thu, 22 Aug 2024 18:33:15 +0200 Subject: [PATCH] Fixed so it checks if the key is set correctly --- .../src/Form/ConfigureAPIKeysForm.php | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/dxpr_cms_installer/src/Form/ConfigureAPIKeysForm.php b/dxpr_cms_installer/src/Form/ConfigureAPIKeysForm.php index 5f1f327..ed4f9fe 100644 --- a/dxpr_cms_installer/src/Form/ConfigureAPIKeysForm.php +++ b/dxpr_cms_installer/src/Form/ConfigureAPIKeysForm.php @@ -200,24 +200,29 @@ public function submitForm(array &$form, FormStateInterface $form_state) { // If the AI provider is set, enable the appropriate modules. if ($ai_provider = $form_state->getValue('ai_provider')) { - // Setup the key for the provider. - $key_id = $ai_provider . '_key'; - $key = Key::create([ - 'id' => $key_id, - 'label' => ucfirst($ai_provider) . ' API Key', - 'description' => 'API Key for ' . ucfirst($ai_provider), - 'key_type' => 'authentication', - 'key_provider' => 'config', - ]); - $key->setKeyValue($form_state->getValue($key_id)); - $key->save(); - // Add the key to the config. - $this->configFactory->getEditable('provider_' . $ai_provider . '.settings')->set('api_key', $key_id)->save(); - // Set the default provider. - $this->configFactory->getEditable('ai.settings')->set('default_providers.chat', [ - 'provider_id' => $ai_provider, - 'model_id' => $ai_provider == 'openai' ? 'gpt-4o' : $this->getFirstAiModelId($ai_provider), - ])->save(); + try { + // Setup the key for the provider. + $key_id = $ai_provider . '_key'; + $key = Key::create([ + 'id' => $key_id, + 'label' => ucfirst($ai_provider) . ' API Key', + 'description' => 'API Key for ' . ucfirst($ai_provider), + 'key_type' => 'authentication', + 'key_provider' => 'config', + ]); + $key->setKeyValue($form_state->getValue($key_id)); + $key->save(); + // Add the key to the config. + $this->configFactory->getEditable('provider_' . $ai_provider . '.settings')->set('api_key', $key_id)->save(); + // Set the default provider. + $this->configFactory->getEditable('ai.settings')->set('default_providers.chat', [ + 'provider_id' => $ai_provider, + 'model_id' => $ai_provider == 'openai' ? 'gpt-4o' : $this->getFirstAiModelId($ai_provider), + ])->save(); + } + catch (\Exception $e) { + $this->messenger()->addError($this->t('An error occurred while saving the AI provider key: @error', ['@error' => $e->getMessage()])); + } } }