You are here

function content_translation_entity_presave in Drupal 8

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

Implements hook_entity_presave().

File

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

Code

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

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