3

I need to recreate the behavior drupal has when you create a translation but programmatically.

Doing $node->addTranslation('de'); on a hook insert create an empty node but I need the field to be filled with the untranslated node content.

Is there a way to achieve this instead of having to repopulate every field from the main node ?

0

1 Answer 1

5

addTranslation takes a second parameter, $values:

An array of initial values to be assigned to the translatable fields. Defaults to none.

So in theory you should just need to make use of that parameter, and pass the node's current field values:

$node->addTranslation('de', $node->toArray());
2
  • 1
    Not only in theory, Drupal does the same to populate new translations, see ContentTranslationController::prepareTranslation
    – 4uk4
    Sep 28, 2018 at 13:33
  • Nice one @4k4, I was pretty sure it did but my clumsy 30 second search didn't turn it up
    – Clive
    Sep 28, 2018 at 13:39

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.