I am trying to add a node translation programmatically when a node is created (using hook_ENTITY_TYPE_insert). My code works fine except that the translation doesn’t really have a content moderation state. My code is as follows:
function mymodule_add_translation($node) {
// get the current language, and determine the translation language
$lang = Drupal::languageManager()->getCurrentLanguage()->getId();
$langcode = ($lang == 'en') ? 'fr' : $lang;
$node->setRevisionTranslationAffected(TRUE);
$node->addTranslation($langcode, ('title' => $node->getTitle()))->save();
}
Here is what the content_moderation_state_field_revision table looks like for two nodes nid:13 I created the node and translated manually and nid:14 I automatically translated using the above code.
I am clearly doing something incorrect, any suggestions would be really appreciated!