ckgpt-1.0.0-alpha1/src/Form/CkGptSettingsForm.php
src/Form/CkGptSettingsForm.php
<?php
namespace Drupal\ckgpt\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class CkGptSettingsForm.
*/
class CkGptSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'ckgpt.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ckgpt_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('ckgpt.settings');
$form['openai_api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('OpenAI API Key'),
'#description' => $this->t('Enter the API key for accessing the OpenAI API.'),
'#default_value' => $config->get('openai_api_key'),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$this->config('ckgpt.settings')
->set('openai_api_key', $values['openai_api_key'])
->save();
parent::submitForm($form, $form_state);
}
}
