11<?php
22
33/**
4- * Implements hook_field_widget_form_alter ().
4+ * Implements hook_element_info_alter ().
55 *
6- * Modify image field widgets to add the "Generate with AI " button.
6+ * Alter the image field widget to include a "Generate Alt Text " button.
77 */
8- function openai_alt_field_widget_form_alter(&$element, &$form_state, $context) {
9- // Only modify image fields.
10- if ($context['field']['type'] != 'image') {
11- return;
8+ function openai_alt_element_info_alter(&$types) {
9+ if (isset($types['managed_file'])) {
10+ $types['managed_file']['#process'][] = 'openai_alt_image_field_process';
1211 }
12+ }
1313
14- // Iterate through all deltas (handles multiple values for image fields).
15- foreach ($element as $delta => &$item) {
16- if (!is_numeric($delta)) {
17- continue;
18- }
19-
20- $fid = $item['#default_value']['fid'] ?? NULL;
21-
22- if (!empty($fid)) {
23- // Generate a unique wrapper ID for the entire field widget.
24- $field_name = $context['field']['field_name'];
25- $wrapper_id = 'ai-image-field-wrapper-' . $field_name . '-' . $delta;
26-
27- // Wrap the entire field widget with a custom wrapper.
28- $item = [
29- '#type' => 'container',
30- '#attributes' => [
31- 'id' => $wrapper_id,
32- 'class' => ['ai-image-field-wrapper'],
33- ],
34- '#prefix' => '', // Ensure #prefix is always defined.
35- '#suffix' => '', // Ensure #suffix is always defined.
36- 'field_content' => $item,
37- ];
38-
39- // Add the "Generate with AI" button at the bottom of the field widget.
40- $item['field_content']['ai_alt_text_generation'] = [
41- '#type' => 'button',
42- '#value' => t('Generate with AI'),
43- '#ajax' => [
44- 'callback' => 'openai_alt_generate_alt_text_ajax',
45- 'wrapper' => $wrapper_id, // Replace the entire field widget wrapper.
46- ],
47- '#attributes' => [
48- 'class' => ['ai-alt-text-generation', 'button--primary'],
49- 'data-file-id' => $fid,
50- 'data-field-name' => $field_name,
51- 'data-delta' => $delta,
52- ],
53- ];
54- }
14+ /**
15+ * Process function to wrap the alt text field and add the AI button.
16+ */
17+ function openai_alt_image_field_process($element, &$form_state, $form) {
18+ if (isset($element['#value']['alt'])) {
19+ $field_name = $element['#field_name'] ?? 'unknown_field';
20+ $delta = $element['#delta'] ?? 0;
21+ //dpm($element);
22+ $original_prefix = $element['#prefix'] ?? ''; // Preserve the original prefix.
23+ $wrapper_id = 'ai-alt-field-wrapper-' . $field_name . '-' . $delta;
24+
25+ // Add your custom wrapper while preserving the original prefix.
26+ $element['#prefix'] = $original_prefix . '<div id="' . $wrapper_id . '" class="ai-alt-field-wrapper">';
27+ $element['#suffix'] = '</div>' . ($element['#suffix'] ?? ''); // Ensure the original suffix is preserved.
28+ //dpm($element);
29+ // Add the "Generate Alt Text" button.
30+ $element['ai_alt_text_generation'] = [
31+ '#type' => 'button',
32+ '#value' => t('Generate Alt Text'),
33+ '#ajax' => [
34+ 'callback' => 'openai_alt_generate_alt_text_ajax',
35+ 'wrapper' => $wrapper_id, // Use your custom wrapper for updates.
36+ ],
37+ '#attributes' => [
38+ 'class' => ['ai-alt-text-generation', 'button--primary'],
39+ 'data-file-id' => $element['#value']['fid'] ?? NULL,
40+ 'data-field-name' => $field_name,
41+ 'data-delta' => $delta,
42+ ],
43+ '#weight' => 50, // Ensure the button appears after the alt field.
44+ ];
5545 }
46+
47+ return $element;
5648}
5749
5850/**
@@ -154,59 +146,54 @@ function openai_alt_generate_alt_text_for_ckeditor($form, &$form_state) {
154146}
155147
156148/**
157- * Get an OpenAI API instance.
158- *
159- * @return OpenAIApi
160- * The OpenAI API instance.
149+ * AJAX callback for generating alt text using OpenAI.
161150 */
162151/**
163- * AJAX callback to generate alt text using OpenAI.
152+ * AJAX callback for generating alt text using OpenAI.
164153 */
165154function openai_alt_generate_alt_text_ajax($form, &$form_state) {
166155 $triggering_element = $form_state['triggering_element'];
167156 $fid = $triggering_element['#attributes']['data-file-id'] ?? NULL;
168157 $field_name = $triggering_element['#attributes']['data-field-name'] ?? NULL;
169158 $delta = $triggering_element['#attributes']['data-delta'] ?? NULL;
170159
171- if (!$fid || !$field_name || $delta === NULL) {
172- backdrop_set_message(t('Missing required information to process alt text.'), 'error');
173- return;
160+ //dpm($triggering_element);
161+
162+ if ($field_name === NULL || $delta === NULL) {
163+ backdrop_set_message(t('Unable to determine field context.'), 'error');
164+ return [
165+ '#type' => 'ajax',
166+ '#commands' => [
167+ ajax_command_alert(t('An error occurred while generating alt text. Please try again.')),
168+ ],
169+ ];
174170 }
175171
176- $file = file_load($fid);
172+ $file = file_load($fid); // Load the file entity using Backdrop's file_load().
177173 if (!$file) {
178174 backdrop_set_message(t('File not found for the given file ID.'), 'error');
179175 return;
180176 }
181177
178+ //dpm($file);
179+
182180 $file_uri = $file->uri;
183181
184- try {
185- $openai_api = openai_alt_get_openai_api();
186- $description = $openai_api->describeImage($file_uri);
187-
188- if (!empty($description)) {
189- // Update the alt text field dynamically.
190- if (!empty($form[$field_name]['und'][$delta]['field_content']['alt'])) {
191- $form[$field_name]['und'][$delta]['field_content']['alt']['#value'] = $description;
192-
193- watchdog('openai_alt', 'Alt text successfully generated: @description', ['@description' => $description], WATCHDOG_INFO);
194- } else {
195- watchdog('openai_alt', 'Alt field not found for field @field_name, delta @delta.', [
196- '@field_name' => $field_name,
197- '@delta' => $delta,
198- ], WATCHDOG_WARNING);
199- }
200- } else {
201- backdrop_set_message(t('Failed to generate alt text.'), 'error');
202- }
203- } catch (Exception $e) {
204- backdrop_set_message(t('An error occurred while generating alt text: @message', ['@message' => $e->getMessage()]), 'error');
205- watchdog('openai_alt', 'Error generating alt text: @error', ['@error' => $e->getMessage()], WATCHDOG_ERROR);
206- }
182+ //dpm($file_uri);
183+
184+ // Generate alt text using OpenAI API.
185+ //$generated_alt_text = 'Generated alt text'; // Replace with OpenAI API logic.
186+
187+ // Get the OpenAIApi instance and describe the image.
188+ $openai_api = openai_alt_get_openai_api();
189+ $generated_alt_text = $openai_api->describeImage($file_uri);
190+ //dpm($generated_alt_text);
207191
208- // Re-render the entire field widget.
209- $wrapper_id = 'ai-image-field-wrapper-' . $field_name . '-' . $delta;
192+ // Update the alt text value.
193+ $form[$field_name]['und'][$delta]['alt']['#value'] = $generated_alt_text;
194+
195+ // Return the updated field.
196+ $wrapper_id = 'ai-alt-field-wrapper-' . $field_name . '-' . $delta;
210197 return [
211198 '#type' => 'ajax',
212199 '#commands' => [
@@ -216,7 +203,6 @@ function openai_alt_generate_alt_text_ajax($form, &$form_state) {
216203}
217204
218205
219-
220206/**
221207 * Get an OpenAI API instance.
222208 *
0 commit comments