Problem/Motivation
This came up as part of the refactoring for #3549153: Translate: use prompt entities instead of custom configurations. In that issue, we transform regular textual prompts being stored in config, to a reference to an ai_prompt config entity, that stores the prompt value and variables information.
In \Drupal\ai_translate\Form\AiTranslateSettingsForm::validateForm() there was already logic to validate that the prompt text has a minimal length of 50 characters.
I'm wondering if this is something that can be generalized: can we assign validation logic to ai_prompt entities of a certain ai_prompt_type, on an entity level?
This would:
- provide a validation framework for similar validation logic (haven't checked if there is similar logic elsewhere in the AI module), instead of one-off code. - already validate the prompt text when the ai_prompt entity gets created, not afterwards when it gets selected. It could then also validate when the prompt gets created outside of the AI Translate settings form (e.g. via code, recipe, general prompt management page, ...) - leverage the Drupal / Symfony validation constraints framework, instead of custom $form_state->setError() calls.
Remaining tasks
- Discuss if this is needed / useful - Investigate where this can be used, within current AI functionality - Provide validation logic on entity / schema level where relevant, instead of via form