function ContentTranslationHooks::entityPresave

Implements hook_entity_presave().

File

core/modules/content_translation/src/Hook/ContentTranslationHooks.php, line 471

Class

ContentTranslationHooks
Hook implementations for content_translation.

Namespace

Drupal\content_translation\Hook

Code

public function entityPresave(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.