diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php index 1ec442f..7cc52ea 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php @@ -325,7 +325,7 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto 'delete' => [], 'entities' => [], ]; - // Store the $items entities in the widget state, for futher manipulation. + // Store the $items entities in the widget state, for further manipulation. $target_langcode = $this->getCurrentLangcode($form_state, $items); foreach ($items as $delta => $item) { if ($item->entity->isTranslatable()) { @@ -491,6 +491,7 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto * {@inheritdoc} */ public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) { + if ($this->canBuildForm($form_state)) { return parent::form($items, $form, $form_state, $get_delta); } @@ -519,7 +520,13 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto // If target translation is not yet available, populate it with data from the original entity. if ($entity->language()->getId() != $target_langcode && !$entity->hasTranslation($target_langcode)) { $original_language = $entity->language()->getId(); - $entity = $entity->addTranslation($target_langcode, $entity->toArray()); + // If reference field is translatable + // instead of adding translation to entity, create a new entity + if ($this->fieldDefinition->isTranslatable()) { + $entity = $entity->createDuplicate(); + } else { + $entity = $entity->addTranslation($target_langcode, $entity->toArray()); + } if ($entity->getEntityType()->isRevisionable()) { $entity->setRevisionTranslationAffected(NULL); } @@ -529,6 +536,10 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto $metadata = $translation_manager->getTranslationMetadata($entity); $metadata->setSource($original_language); } + // If reference field is translatable + if ($this->fieldDefinition->isTranslatable()) { + return $entity; + } // Return the entity with the correct translation. return $entity->getTranslation($target_langcode); }