|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Implements hook_config_info(). |
| 5 | + */ |
| 6 | +function openai_alt_config_info() { |
| 7 | + return [ |
| 8 | + 'openai_alt.settings' => [ |
| 9 | + 'label' => t('OpenAI Alt Settings'), |
| 10 | + 'group' => t('Configuration'), |
| 11 | + ], |
| 12 | + ]; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Implements hook_menu(). |
| 17 | + */ |
| 18 | +function openai_alt_menu() { |
| 19 | + $items = []; |
| 20 | + |
| 21 | + $items['admin/config/openai/openai-alt'] = [ |
| 22 | + 'title' => 'OpenAI Alt Text Generation', |
| 23 | + 'description' => 'Provides the possibility to fill out the alt text of an image field using OpenAI.', |
| 24 | + 'page callback' => 'backdrop_get_form', |
| 25 | + 'page arguments' => ['openai_alt_settings_form'], |
| 26 | + 'access arguments' => ['administer site configuration'], |
| 27 | + 'parent' => 'admin/config/openai', |
| 28 | + ]; |
| 29 | + |
| 30 | + return $items; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Build the configuration form for OpenAI Alt Text. |
| 35 | + * |
| 36 | + * @return array |
| 37 | + * The configuration form. |
| 38 | + */ |
| 39 | +function openai_alt_settings_form(array $form, array &$form_state) { |
| 40 | + // Get the configuration object to load values. |
| 41 | + $config = config('openai_alt.settings'); |
| 42 | + |
| 43 | + $openai_config = config('openai.settings'); |
| 44 | + $apiKey = key_get_key_value($openai_config->get('api_key')); |
| 45 | + |
| 46 | + if (empty($apiKey)) { |
| 47 | + form_set_error('', t('API key is not set. Please configure the API key in OpenAI settings.')); |
| 48 | + return $form; |
| 49 | + } |
| 50 | + |
| 51 | + $api = new OpenAIApi($apiKey); |
| 52 | + |
| 53 | + $form = array(); |
| 54 | + $form['#config'] = 'openai_alt.settings'; |
| 55 | + |
| 56 | + // Retrieve configuration values. |
| 57 | + $prompt = $config->get('prompt'); |
| 58 | + |
| 59 | + // Prompt for generating alt text. |
| 60 | + $form['prompt'] = [ |
| 61 | + '#type' => 'textarea', |
| 62 | + '#title' => t('OpenAI Alt text generation prompt'), |
| 63 | + '#default_value' => $prompt, |
| 64 | + '#description' => t('Prompt used for generating the alt text.'), |
| 65 | + '#required' => TRUE, |
| 66 | + '#rows' => 15, // Adjust the height of the textarea. |
| 67 | + '#cols' => 80, // Adjust the width of the textarea. |
| 68 | + '#attributes' => [ |
| 69 | + 'style' => 'white-space: pre-wrap;', // Ensure newlines and spaces are preserved. |
| 70 | + ], |
| 71 | + ]; |
| 72 | + |
| 73 | + // Model section |
| 74 | + |
| 75 | + $models = $api->filterModels(['gpt-4o|gpt-4-turbo|vision']); |
| 76 | + |
| 77 | + $form['model'] = [ |
| 78 | + '#type' => 'select', |
| 79 | + '#title' => t('Model to use'), |
| 80 | + '#options' => $models, |
| 81 | + '#default_value' => $config->get('model'), |
| 82 | + '#required' => TRUE, |
| 83 | + '#description' => t('AI model to use for generating the alt text.'), |
| 84 | + ]; |
| 85 | + |
| 86 | + return system_settings_form($form); |
| 87 | +} |
| 88 | + |
3 | 89 | /** |
4 | 90 | * Implements hook_element_info_alter(). |
5 | 91 | * |
|
0 commit comments