1

I am adding translation to a node programmatically using a script which I run with drush scr.

Everything works as far as adding translation and copying all the fields over. However, I am not able to set the Created time and the Author of the translation. No error is thrown and the result is just the author and created time of the original content. My code is below:

$node = node_load($nid);
$source_translation = $node->getTranslation($sourceLanguageKey);
$target_translation = $node->addTranslation($translationLanguageKey, $source_translation->toArray());

// Make sure we do not inherit the affected status from the source values.
if ($node->getEntityType()->isRevisionable()) {
    $target_translation->setRevisionTranslationAffected(NULL);
}

$entityManager = \Drupal::getContainer()->get('entity.manager');
$user = $entityManager->getStorage('user')->load(45986);

$translationManager = \Drupal::getContainer()->get('content_translation.manager');
$metadata = $translationManager->getTranslationMetadata($target_translation);
$metadata->setAuthor($user); // this does not work
$metadata->setCreatedTime(REQUEST_TIME); // this does not work
$metadata->setSource($sourceLanguageKey);
$node->save();

I am using the metadata object to set author, created time and source. Both author and create time failed to set but the source was set properly. Any idea why it is not working? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.