function ContentTranslationHooks::entityPresave

Implements hook_entity_presave().

Attributes

#[Hook('entity_presave')]

File

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

Class

ContentTranslationHooks
Hook implementations for content_translation.

Namespace

Drupal\content_translation\Hook

Code

public function entityPresave(EntityInterface $entity) : void {
  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && !$entity->isNew() && $entity->getOriginal()) {
    /** @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->getOriginal()
      ->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.