You are here

protected function ContentImporter::updateTranslation in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Importer/ContentImporter.php \Drupal\content_sync\Importer\ContentImporter::updateTranslation()

Updates translations.

Parameters

$entity: An entity object.

\Drupal\Core\Entity\ContentEntityType $entity_type: A ContentEntityType object.

array $entity_translations: An array of translations.

$context: The batch context.

1 call to ContentImporter::updateTranslation()
ContentImporter::importEntity in src/Importer/ContentImporter.php

File

src/Importer/ContentImporter.php, line 101

Class

ContentImporter

Namespace

Drupal\content_sync\Importer

Code

protected function updateTranslation(&$entity, $entity_type, $entity_translations, $context) {
  foreach ($entity_translations as $langcode => $translation) {

    // Denormalize.
    $translation = $this->serializer
      ->denormalize($translation, $entity_type
      ->getClass(), $this->format, $context);

    // If an entity has a translation - update one, otherwise - add a new one.
    $entity_translation = $entity
      ->hasTranslation($langcode) ? $entity
      ->getTranslation($langcode) : $entity
      ->addTranslation($langcode);

    // Get fields definitions.
    $fields = $translation
      ->getFieldDefinitions();
    foreach ($translation as $itemID => $item) {
      if ($entity_translation
        ->hasField($itemID)) {
        if ($fields[$itemID]
          ->isTranslatable() == TRUE) {
          $entity_translation->{$itemID}
            ->setValue($item
            ->getValue());
        }
      }
    }

    // Avoid issues updating revisions.
    if ($entity_translation
      ->getEntityType()
      ->hasKey('revision')) {
      $entity_translation
        ->updateLoadedRevisionId();
      $entity_translation
        ->setNewRevision(FALSE);
    }

    // Save the entity translation.
    $entity_translation
      ->save();
  }
}