I need to import some custom field data from a Drupal 6 instance into a Drupal 8 instance using Paragraphs. Since D8 stores all translations in a single node instead of separate linked nodes like D6, I am having an problem trying to get translated content into a Paragraph and inserted into a node (page.) The default language (English) works fine. Here is my code (I am importing from a JSON file that was a dump from the D6 instance):
... Code to read JSON file here and load into $data variable ...
// create paragraph ($fields is an array of fields from the JSON file)
$paragraph = Paragraph::create($fields);
// load existing node
$node = Node::load($nodeId);
// Get the translated node...according to the docs, this should
// return a node that behaves just like the original node
$language = \Drupal::languageManager()->getLanguage($data['language'])->getId();
$node = $node->getTranslation($language);
$paragraphs = $node->field_paragraph_group;
$paragraphs[] = $paragraph;
$node->field_paragraph_group = $paragraphs;
$node->save();
It appears to save ok but once this is run the website stops working with
The website encountered an unexpected error. Please try again later.
so it's obviously breaking something.
I'm not sure if I need to try and access the translation from the node or add the translation in the Paragraph object, something like:
$paragraph->language = $data['language'];
Any guidance would be appreciated! Thank you!