Populates target values with the source values.
\Drupal\Core\Entity\EntityInterface $entity: The entitiy being translated.
\Drupal\Core\Language\Language $source: The language to be used as source.
\Drupal\Core\Language\Language $target: The language to be used as target.
function translation_entity_prepare_translation(EntityInterface $entity, Language $source, Language $target) {
// @todo Unify field and property handling.
$instances = field_info_instances($entity
->entityType(), $entity
->bundle());
if ($entity instanceof EntityNG) {
$source_translation = $entity
->getTranslation($source->langcode);
$target_translation = $entity
->getTranslation($target->langcode);
foreach ($target_translation
->getPropertyDefinitions() as $property_name => $definition) {
// @todo The value part should not be needed. Remove it as soon as things
// do not break.
$target_translation->{$property_name}->value = $source_translation->{$property_name}->value;
}
}
else {
foreach ($instances as $field_name => $instance) {
$field = field_info_field($field_name);
if (!empty($field['translatable'])) {
$value = $entity
->get($field_name);
$value[$target->langcode] = isset($value[$source->langcode]) ? $value[$source->langcode] : array();
$entity
->set($field_name, $value);
}
}
}
}