Problem/Motivation

NodeAudioGenerateForm::submitForm() resolves the provider settings for a node's audio file (provider, TTS configuration, voice instructions, Google TTS settings, filename template, media field, CloudConvert key), then immediately builds and runs the batch. There is currently no way for another module to inspect or adjust any of this before the batch operations are built — for example, to derive the TTS language from the node's own content language/translation instead of a fixed per-provider configuration value, to swap the target media field conditionally, to inject/reorder text chunks, or to disable the CloudConvert conversion step for specific content.

Proposed resolution

Introduce a new hook, hook_ai_audio_generator_pre_execute(array &$context, \Drupal\node\NodeInterface $node, array $file_config), invoked in NodeAudioGenerateForm::submitForm() right before the batch operations (processChunk, mergeChunks, convertWithCloudConvert) are built. $context is passed by reference and bundles everything the batch is about to run with:

- chunks — the ordered list of text chunks to convert to speech
- use_ssml — whether the text should be treated as SSML (Google TTS)
- tts_provider — the provider_id__model_id string (AI providers)
- tts_configuration — provider-specific TTS configuration (e.g. voice)
- voice_instructions — the voice instructions prompt (AI providers)
- batch_provider_settings — provider_type, google_tts_key_id, google_tts_language_code, google_tts_voice_name
- filename_template — the filename template for the merged audio file
- media_field — the media field the merged audio is saved to
- cloudconvert_key_id — Key module key ID for CloudConvert, or empty to skip conversion

Since the hook mutates data by reference but is not an "alter" of an existing structure in the traditional sense, it intentionally does not use the _alter suffix/convention. It's invoked via ModuleHandler::invokeAll() with an explicit by-reference argument ([&$context, $node, $file_config]) — the same technique Drupal core itself uses for hook_entity_view().

A worked example (deriving the Google TTS language code from the node's content language, falling back to the configured value when the node has no specific language) is documented in ai_audio_generator.api.php.

API changes

Adds a new hook: hook_ai_audio_generator_pre_execute().

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

nnevill created an issue. See original summary.

  • nnevill committed 219e451b on 1.0.x
    feat: #3618324 Add a hook to customize audio generation before it runs
    
nnevill’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.