Add AJAX functionality to dynamically update fields based on provider and model

Created on 7 May 2025, about 1 month ago

Problem/Motivation

The AI Media Image module currently doesn't dynamically update configuration fields when switching between different AI providers or models. When a user changes the AI provider or selects a different model, the configuration options (like image size, response format, etc.) don't update to reflect the capabilities of the selected provider/model.

This is particularly problematic when switching between models that support different image sizes (e.g., DALL-E 2 vs DALL-E 3), as users may be presented with incompatible options.

Proposed Solution

This patch implements AJAX functionality to dynamically update the form elements when changing providers or models:

  1. Adds AJAX callbacks for both provider and model select fields
  2. Updates the model options when the provider is changed
  3. Updates configuration fields like image size and response format based on the selected model
  4. Adds special handling for provider-specific options (OpenAI and Stability AI)
  5. Includes proper error handling and logging

Benefits

  • Improved user experience with dynamically updated fields
  • Prevents errors from submitting incompatible configuration options
  • Maintains parity with similar functionality in the ai_audio_field 🐛 C Active module
  • Ensures proper defaults are selected based on the capabilities of each model
Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇰🇬Kyrgyzstan dan_metille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dan_metille
  • 🇧🇪Belgium svendecabooter Gent

    Tested with OpenAI, and getting the following errors:

    Warning: Undefined array key "fids" in Drupal\media_library\Form\FileUploadForm->validateUploadElement() (line 212 of /var/www/html/web/core/modules/media_library/src/Form/FileUploadForm.php)

    ypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 212 of /var/www/html/web/core/modules/media_library/src/Form/FileUploadForm.php).

    Haven't investigated the root cause yet...

  • 🇧🇪Belgium svendecabooter Gent

    EDIT: seems to be unrelated to this patch, because I'm getting the same errors when patch is not applied.

Production build 0.71.5 2024