function translation_entity_prepare_translation

Populates target values with the source values.

Parameters

\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.

1 call to translation_entity_prepare_translation()
translation_entity_add_page in drupal/core/modules/translation_entity/translation_entity.pages.inc
Page callback for the translation addition page.

File

drupal/core/modules/translation_entity/translation_entity.pages.inc, line 205
The entity translation user interface.

Code

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);
      }
    }
  }
}