AI Translate: LbFieldExtractor adds translations with default values for empty fields.

Created on 1 September 2025, about 1 month ago

Problem/Motivation

AI Translate: LbFieldExtractor adds translations with field's default values if these fields are empty in the original block entity.

Steps to reproduce (required for bugs, but not feature requests)

Please provide information like AI modules enabled, which AI provider, browser, etc.
Let's say we have a block component having multiple fields. In the field settings we have set some default values for these fields, but these fields are not required.
So when we add the block and keep any field blank and also remove the default values and saves it, and then we add generate the AI translations, The fields which are empty empty are now filled with field's default values in the translated version.

Proposed resolution

Change the code from

      $blockEntity = $blockEntity->hasTranslation($translationLanguage)
        ? $blockEntity->getTranslation($translationLanguage)
        : $blockEntity->addTranslation($translationLanguage);

to

     $blockEntity = $blockEntity->hasTranslation($translationLanguage)
        ? $blockEntity->getTranslation($translationLanguage)
        : $blockEntity->addTranslation($translationLanguage, $blockEntity->toArray());

Remaining tasks

Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)

๐Ÿ› Bug report
Status

Active

Version

1.2

Component

AI Translate

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia anmolgoyal74

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

Merge Requests

Comments & Activities

  • Issue created by @anmolgoyal74
  • ๐Ÿ‡ง๐Ÿ‡ฌBulgaria valthebald Sofia

    @anmolgoyal74: that's a good proposal! Do you want to create a merge request, or someone else can?

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Ishani Patel

    ishani patel โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Ishani Patel

    Hello,
    I've created a Merge request.
    Please check and review.

    Thank you!

  • Pipeline finished with Success
    29 days ago
    Total: 242s
    #587706
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anmolgoyal74

    The same solution also needs to be applied in ReferenceFieldExtractor.php and AiTranslateCommands.php.

    --- a/modules/ai_translate/src/Plugin/FieldTextExtractor/ReferenceFieldExtractor.php
    +++ b/modules/ai_translate/src/Plugin/FieldTextExtractor/ReferenceFieldExtractor.php
    @@ -184,7 +184,7 @@ class ReferenceFieldExtractor extends FieldExtractorBase implements Configurable
     
         // Check if the entity has the translation, create it if not.
         if (!$entity->hasTranslation($language)) {
    -      $translatedEntity = $entity->addTranslation($language);
    +      $translatedEntity = $entity->addTranslation($language, $entity->toArray());
         }
         else {
           $translatedEntity = $entity->getTranslation($language);
    
    
    --- a/modules/ai_translate/src/Drush/AiTranslateCommands.php
    +++ b/modules/ai_translate/src/Drush/AiTranslateCommands.php
    @@ -119,7 +119,7 @@ class AiTranslateCommands extends DrushCommands {
             $translated_text_item = html_entity_decode($translated_text_item);
           }
         }
    -    $translation = $entity->addTranslation($langTo);
    +    $translation = $entity->addTranslation($langTo, $entity->toArray());
         $this->textExtractor->insertTextMetadata($translation,
           $textMetadata);
         try {
    
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium svendecabooter Gent

    anmolgoyal74: can you add this to the MR?

    The change in modules/ai_translate/src/Plugin/FieldTextExtractor/ReferenceFieldExtractor.php did fix the issue I encountered on a website, where untranslated fields (eg regular select list) where left blank in the AI translation.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anmolgoyal74

    Applied the changes to ReferenceFieldExtractor.php and AiTranslateCommands.php..

  • Pipeline finished with Success
    23 days ago
    Total: 351s
    #593226
Production build 0.71.5 2024