Created on 26 April 2025, 15 days ago

Problem/Motivation

When editing content with an AI Audio field, changing the "Provider" dropdown does not update the available options in the "Model" and "Voice" dropdowns. This makes it impossible to change providers after initial creation.

Steps to reproduce

  • Install the AI Audio Field module
  • Create a content type with an AI Audio field
  • Create a node with this content type and select a provider, model, and voice
  • Edit the node and try to change the provider

Current behavior
When changing the Provider dropdown, the Model and Voice options remain the same as the previously selected provider. Upon saving, this triggers validation errors because the selected model and voice are not valid for the new provider.

Expected behavior
When changing the Provider dropdown, the Model and Voice dropdowns should be updated via AJAX to show only the options available for the newly selected provider.

Proposed resolution

Add AJAX callback to the Provider dropdown to refresh the form when the provider is changed, ensuring that the Model and Voice options are updated to reflect the available options for the selected provider.

🐛 Bug report
Status

Active

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
  • 🇰🇬Kyrgyzstan dan_metille

    I’ve tried to digg into both this issue #3521351 and the related #3521350 (remove button in multivalue widgets).
    The attached patch captures the current state of work so others can test/review.

    What the patch does

    • Adds a proper AJAX callback to the Provider select
    • Rebuilds the entire widget wrapper so the Model and Voice lists refresh when the provider changes.
    • Synchronises widget-state values (provider / model) on every rebuild and in generateSubmit().
    • Falls back to the first available model if the user never touches the Model list.

    Current test results

    • Model / Voice lists do update when switching providers.

    But:

    • After changing provider and clicking Generate audio, the form still reloads without producing a file and silently reverts to the original provider.
    • When provider is changed via AJAX, the Drupal-core remove control briefly reappears, leaving us with two Remove buttons again until the next full reload.

    So the patch is an improvement, but it does not completely fix either issue yet.
    Any feedback or follow-up patches very welcome!

  • 🇰🇬Kyrgyzstan dan_metille

    The attached patch addresses these two reported issues in the AI Audio Field module:

    1. Issue #3521351: Fixed audio not generating properly after changing the AI provider (current issue)
    2. Issue #3521350: Fixed the missing remove buttons for unlimited cardinality fields (related issue)

    The patch includes the following improvements:

    Remove Button Fix

    • Added proper implementation of remove buttons for multi-value fields following Drupal core patterns
    • Implemented correct AJAX callbacks and submit handlers for the remove functionality
    • Fixed button visibility and positioning that was broken during AJAX operations

    Audio Generation Fix

    • Fixed form state management to properly handle provider changes
    • Improved AJAX callbacks to return the correct form elements
    • Added proper JavaScript support to maintain field state during AJAX operations
    • Enhanced audio player rendering using HTML5 elements and proper caching controls

    Additional Improvements

    • Fixed widget state management to maintain values across AJAX requests
    • Added proper initialization for new field items with default provider values
    • Improved widget rebuilding for dependent fields (model, configuration options)
    • Added comprehensive logging for easier debugging and maintenance

    Testing

    The patch has been tested in both Drupal 10 environment with the following test cases:

    • Creating new AI audio fields
    • Adding multiple field items
    • Removing field items
    • Changing providers and verifying dependent fields update properly
    • Generating audio with different providers
    • Verifying audio playback works correctly after AJAX operations

    Notes

    This patch follows Drupal best practices by using:

    • Proper form state management via WidgetState API
    • Standard form AJAX callbacks and wrappers
    • Drupal's form API for widget elements
    • Standard HTML5 audio elements with proper attributes
    • JavaScript behaviors with proper DOM event handling

    The patch is ready for review and should resolve both issues completely.

  • 🇰🇬Kyrgyzstan dan_metille

    Now the patch has been updated to include the fix for the "Call to undefined function element_children()" error. The changes include:

    1. Added the proper import: use Drupal\Core\Render\Element;
    2. Updated the function call to use the namespaced version: Element::children($element)

    This will fix the error encountered when visiting the Media content page. The patch now addresses both original issues (#3521350 and #3521351) plus this new issue with the undefined function.

  • 🇰🇬Kyrgyzstan dan_metille

    The updated patch now includes:

    1. Changed element_children() to Element::children() with proper import
    2. Added method_exists() check before calling getFieldDefinition() to handle Search API fields

    The patch is now comprehensive and addresses all three issues:

    1. Original issue #3521350: Remove button functionality for unlimited cardinality fields
    2. Original issue #3521351: Audio not generating properly after changing AI providers
    3. New issues: Element function imports and proper handling of Search API fields

    These changes make the module more robust and compatible with other Drupal modules like Search API.

  • 🇰🇬Kyrgyzstan dan_metille

    Updated patch

Production build 0.71.5 2024