Skip to content

Commit 71b883c

Browse files
authored
Merge pull request #42 from keiserjb/1.x-1.x
add weights for filter format editor image form
2 parents 4d3ad8f + b10b9e8 commit 71b883c

File tree

1 file changed

+41
-99
lines changed

1 file changed

+41
-99
lines changed

modules/openai_alt/openai_alt.module

Lines changed: 41 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,12 @@
11
<?php
22

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-
893
/**
904
* Implements hook_element_info_alter().
915
*
926
* Alter the image field widget to include a "Generate Alt Text" button.
937
*/
94-
function openai_alt_element_info_alter(&$types) {
8+
function openai_alt_element_info_alter(&$types)
9+
{
9510
if (isset($types['managed_file'])) {
9611
$types['managed_file']['#process'][] = 'openai_alt_image_field_process';
9712
}
@@ -100,7 +15,8 @@ function openai_alt_element_info_alter(&$types) {
10015
/**
10116
* Process function to wrap the alt text field and add the AI button.
10217
*/
103-
function openai_alt_image_field_process($element, &$form_state, $form) {
18+
function openai_alt_image_field_process($element, &$form_state, $form)
19+
{
10420
if (isset($element['#value']['alt'])) {
10521
$field_name = $element['#field_name'] ?? 'unknown_field';
10622
$delta = $element['#delta'] ?? 0;
@@ -136,9 +52,33 @@ function openai_alt_image_field_process($element, &$form_state, $form) {
13652
/**
13753
* Implements hook_form_alter().
13854
*/
139-
function openai_alt_form_alter(&$form, &$form_state, $form_id) {
55+
function openai_alt_form_alter(&$form, &$form_state, $form_id)
56+
{
14057
if ($form_id == 'filter_format_editor_image_form') {
141-
// Ensure the 'alt' field exists.
58+
// Add weights for existing fields.
59+
if (isset($form['image'])) {
60+
$form['image']['fid']['#weight'] = 0; // File upload field.
61+
$form['image']['src']['#weight'] = 1; // Image source field.
62+
}
63+
if (isset($form['alt'])) {
64+
$form['alt']['#weight'] = 2; // Alternative text field.
65+
}
66+
if (isset($form['size'])) {
67+
$form['size']['#weight'] = 4; // Image size wrapper.
68+
$form['size']['width']['#weight'] = 5; // Width field.
69+
$form['size']['height']['#weight'] = 6; // Height field.
70+
}
71+
if (isset($form['align'])) {
72+
$form['align']['#weight'] = 7; // Alignment field.
73+
}
74+
if (isset($form['caption'])) {
75+
$form['caption']['#weight'] = 8; // Caption checkbox.
76+
}
77+
if (isset($form['actions'])) {
78+
$form['actions']['submit']['#weight'] = 8; // Submit button.
79+
}
80+
81+
// Ensure the 'alt' field exists before adding the Generate with AI button.
14282
if (isset($form['alt'])) {
14383
// Add a unique ID to the `alt` field for AJAX targeting.
14484
$form['alt']['#prefix'] = '<div id="edit-alt-wrapper">';
@@ -155,6 +95,7 @@ function openai_alt_form_alter(&$form, &$form_state, $form_id) {
15595
'#attributes' => [
15696
'class' => ['ckeditor-ai-generate-button'],
15797
],
98+
'#weight' => 3, // Place the button right after the alt field.
15899
];
159100
}
160101
}
@@ -164,7 +105,8 @@ function openai_alt_form_alter(&$form, &$form_state, $form_id) {
164105
/**
165106
* AJAX callback to generate alt text in CKEditor modal.
166107
*/
167-
function openai_alt_generate_alt_text_for_ckeditor($form, &$form_state) {
108+
function openai_alt_generate_alt_text_for_ckeditor($form, &$form_state)
109+
{
168110
// Retrieve the file ID (`fid`) from the form.
169111
$fid = $form['image']['fid']['#value']['fid'] ?? NULL;
170112

@@ -206,12 +148,10 @@ function openai_alt_generate_alt_text_for_ckeditor($form, &$form_state) {
206148
ajax_command_replace('#edit-alt-wrapper', backdrop_render($form['alt'])),
207149
],
208150
];
209-
}
210-
else {
151+
} else {
211152
backdrop_set_message(t('Failed to generate alt text. Please try again.'), 'error');
212153
}
213-
}
214-
catch (Exception $e) {
154+
} catch (Exception $e) {
215155
// Log the error and notify the user.
216156
watchdog('openai_alt', 'Error generating alt text: @error', ['@error' => $e->getMessage()], WATCHDOG_ERROR);
217157
return [
@@ -237,7 +177,8 @@ function openai_alt_generate_alt_text_for_ckeditor($form, &$form_state) {
237177
/**
238178
* AJAX callback for generating alt text using OpenAI.
239179
*/
240-
function openai_alt_generate_alt_text_ajax($form, &$form_state) {
180+
function openai_alt_generate_alt_text_ajax($form, &$form_state)
181+
{
241182
$triggering_element = $form_state['triggering_element'];
242183
$fid = $triggering_element['#attributes']['data-file-id'] ?? NULL;
243184
$field_name = $triggering_element['#attributes']['data-field-name'] ?? NULL;
@@ -261,7 +202,7 @@ function openai_alt_generate_alt_text_ajax($form, &$form_state) {
261202
return;
262203
}
263204

264-
// dpm($file);
205+
// dpm($file);
265206

266207
$file_uri = $file->uri;
267208

@@ -273,7 +214,7 @@ function openai_alt_generate_alt_text_ajax($form, &$form_state) {
273214
// Get the OpenAIApi instance and describe the image.
274215
$openai_api = openai_alt_get_openai_api();
275216
$generated_alt_text = $openai_api->describeImage($file_uri);
276-
//dpm($generated_alt_text);
217+
dpm($generated_alt_text);
277218

278219
// Update the alt text value.
279220
$form[$field_name]['und'][$delta]['alt']['#value'] = $generated_alt_text;
@@ -295,7 +236,8 @@ function openai_alt_generate_alt_text_ajax($form, &$form_state) {
295236
* @return OpenAIApi
296237
* The OpenAI API instance.
297238
*/
298-
function openai_alt_get_openai_api() {
239+
function openai_alt_get_openai_api()
240+
{
299241
static $openai_api;
300242
if (!$openai_api) {
301243
$api_key = key_get_key_value(config('openai.settings')->get('api_key'));

0 commit comments

Comments
 (0)