You are here

public function ContentExporter::exportEntity in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Exporter/ContentExporter.php \Drupal\content_sync\Exporter\ContentExporter::exportEntity()

@inheritdoc

Overrides ContentExporterInterface::exportEntity

File

src/Exporter/ContentExporter.php, line 28

Class

ContentExporter

Namespace

Drupal\content_sync\Exporter

Code

public function exportEntity(ContentEntityInterface $entity, array $context = []) {
  $context = $this->context + $context;
  $context += [
    'content_sync' => TRUE,
  ];

  // Allows to know to normalizers that this is a content sync generated entity.
  $entity->is_content_sync = TRUE;
  $normalized_entity = $this->serializer
    ->serialize($entity, $this->format, $context);

  //    $return = [
  //      'entity_type_id' => $entity->getEntityTypeId(),
  //      'entity' => $this->serializer->encode($normalized_entity, $this->format, $context),
  //      'original_entity' => $entity,
  //    ];
  // Include translations to the normalized entity
  $yaml_parsed = Yaml::decode($normalized_entity);
  $lang_default = $entity
    ->language()
    ->getId();
  foreach ($entity
    ->getTranslationLanguages() as $langcode => $language) {

    // Verify that it is not the default langcode.
    if ($langcode != $lang_default) {
      if ($entity
        ->hasTranslation($langcode)) {
        $entity_translated = $entity
          ->getTranslation($langcode);
        $normalized_entity_translations = $this->serializer
          ->serialize($entity_translated, $this->format, $context);

        //$normalized_data['_translations'][$c] = $contentExporter->exportEntity($object_translated, $serializer_context);
        $yaml_parsed['_translations'][$langcode] = Yaml::decode($normalized_entity_translations);
      }
    }
  }
  return Yaml::encode($yaml_parsed);
}