function content_translation_entity_presave

Same name in other branches
  1. 9 core/modules/content_translation/content_translation.module \content_translation_entity_presave()
  2. 10 core/modules/content_translation/content_translation.module \content_translation_entity_presave()
  3. 11.x 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

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.