Problem/Motivation
after applying the fix from
π
Translations not exported properly
Closed: duplicate
to properly export translations, I ran into the following error when trying to import content with translatable (asymmetric) paragraphs:
Drupal\Core\Entity\EntityStorageException: Invalid translation language (und) specified. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (regel 811 van /var/www/html/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
generated by the save() method in the following code segment:
// Sync translations of the entity.
if (isset($content['translations']) && $entity instanceof TranslatableInterface) {
foreach ($content['translations'] as $langcode => $translation_content) {
$translated_entity = !$entity->hasTranslation($langcode) ? $entity->addTranslation($langcode) : $entity->getTranslation($langcode);
$this->importBaseValues($translated_entity, $translation_content['base_fields']);
$this->importCustomValues($translated_entity, $translation_content['custom_fields']);
$translated_entity->save();
}
}
the problem seems to be related to this core issue:
https://www.drupal.org/project/drupal/issues/3226689 β
since the $translated_entity is created using addTranslation() method which will not set the proper content_translation_source property
Steps to reproduce
Have a site with at least 2 languages.
Create a simple content type with a paragraph reference field: make the field translatable and use the experimental asymmetric widget.
Create a content and export it with translations. Then try to import it on another site with similar configuration.
Proposed resolution
I was able to work around the issue by setting the content_translation_source property manually, before saving the translations:
$translated_entity->set('content_translation_source', $entity->language()->getId());