content_translation_entity_presave

content_translation_entity_presave(EntityInterface $entity)

Implements hook_entity_presave().

File

core/modules/content_translation/content_translation.module, line 409
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 = \Drupal::entityTypeManager()
        ->getStorage($entity->entityType())->loadUnchanged($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);
  }
}
doc_Drupal
2016-10-29 08:58:37
Comments
Leave a Comment

Please login to continue.