protected function ContentTranslationPreviewController::makePreview

Builds the entity translation for the provided translation data.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity for which the translation should be returned.

array $data: The translation data for the fields.

string $target_langcode: The target language.

Return value

\Drupal\Core\Entity\ContentEntityInterface Translation data.

1 call to ContentTranslationPreviewController::makePreview()
ContentTranslationPreviewController::preview in sources/content/src/Controller/ContentTranslationPreviewController.php
Preview job item entity data.

File

sources/content/src/Controller/ContentTranslationPreviewController.php, line 115

Class

ContentTranslationPreviewController
Content preview translation controller.

Namespace

Drupal\tmgmt_content\Controller

Code

protected function makePreview(ContentEntityInterface $entity, array $data, $target_langcode) {

  // If the translation for this language does not exist yet, initialize it.
  if (!$entity
    ->hasTranslation($target_langcode)) {
    $entity
      ->addTranslation($target_langcode, $entity
      ->toArray());
  }
  $embeded_fields = $this
    ->config('tmgmt_content.settings')
    ->get('embedded_fields');
  $translation = $entity
    ->getTranslation($target_langcode);
  foreach (Element::children($data) as $name) {
    $field_data = $data[$name];
    foreach (Element::children($field_data) as $delta) {
      $field_item = $field_data[$delta];
      foreach (Element::children($field_item) as $property) {
        $property_data = $field_item[$property];

        // If there is translation data for the field property, save it.
        if (isset($property_data['#translation']['#text']) && $property_data['#translate'] && is_numeric($delta)) {
          $item = $translation
            ->get($name)
            ->offsetGet($delta);
          if ($item) {
            $translation
              ->get($name)
              ->offsetGet($delta)
              ->set($property, $property_data['#translation']['#text']);
          }
        }
        elseif (isset($embeded_fields[$entity
          ->getEntityTypeId()][$name])) {
          $this
            ->makePreview($translation
            ->get($name)
            ->offsetGet($delta)->{$property}, $property_data, $target_langcode);
        }
      }
    }
  }
  return $translation;
}