2

I have a question about create node translation programmatically.

First of all, the following is the database screenshot when we use drupal UI to create the translation.

The revision_translation_affected will be set to the language each time when the new one is created.

For example, I create an English article and then translate it with Chinese.

enter image description here

We can see the revision translation affected has been set with the Chinese version because of the translation is the latest created.

However, I use the following code to create a node and add the translation.

$post = \Drupal::entityTypeManager()->getStorage('node')->create(
        [
            'type' => 'page',
            'title' => 'test title',
            'langcode' => 'en'
        ]
    );
    $post->save();
    $source_translation = $post->getTranslation('en');
    $target_translation = $post->addTranslation('zh-hant', $source_translation->toArray());
    $target_translation->save();

I got the following database result

enter image description here

This result will cause the revision confusion when we want to see the revision record from each translation.

Does anyone have experience on how to handle this issue?

1 Answer 1

1

I just ran into this and this prevented my translated revision also appearing as a revision of my original:

$langcode = 'fr';
$node->setRevisionTranslationAffected(FALSE);
$node->addTranslation($langcode, ['title' => $node->getTitle()])->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.