You are here

function content_translation_entity_presave in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/content_translation/content_translation.module \content_translation_entity_presave()

Implements hook_entity_presave().

File

core/modules/content_translation/content_translation.module, line 401
Allows entities to be translated into different languages.

Code

function content_translation_entity_presave(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface && $entity
    ->isTranslatable() && !$entity
    ->isNew()) {

    // If we are creating a new translation we need to use the source language
    // as original language, since source values are the only ones available to
    // compare against.
    if (!isset($entity->original)) {
      $entity->original = entity_load_unchanged($entity
        ->entityType(), $entity
        ->id());
    }
    $langcode = $entity
      ->language()
      ->getId();

    /** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
    $manager = \Drupal::service('content_translation.manager');
    $source_langcode = !$entity->original
      ->hasTranslation($langcode) ? $manager
      ->getTranslationMetadata($entity)
      ->getSource() : NULL;
    \Drupal::service('content_translation.synchronizer')
      ->synchronizeFields($entity, $langcode, $source_langcode);
  }
}