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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!content_translation!content_translation.module/function/content_translation_entity_presave/8.1.x