ContentExporter.php in Content Synchronization 8.2
File
src/Exporter/ContentExporter.php
View source
<?php
namespace Drupal\content_sync\Exporter;
use Drupal\Core\Entity\ContentEntityInterface;
use Symfony\Component\Serializer\Serializer;
use Drupal\Component\Serialization\Yaml;
class ContentExporter implements ContentExporterInterface {
protected $format = 'yaml';
protected $serializer;
protected $context = [];
public function __construct(Serializer $serializer) {
$this->serializer = $serializer;
}
public function exportEntity(ContentEntityInterface $entity, array $context = []) {
$context = $this->context + $context;
$context += [
'content_sync' => TRUE,
];
$entity->is_content_sync = TRUE;
$normalized_entity = $this->serializer
->serialize($entity, $this->format, $context);
$yaml_parsed = Yaml::decode($normalized_entity);
$lang_default = $entity
->language()
->getId();
foreach ($entity
->getTranslationLanguages() as $langcode => $language) {
if ($langcode != $lang_default) {
if ($entity
->hasTranslation($langcode)) {
$entity_translated = $entity
->getTranslation($langcode);
$normalized_entity_translations = $this->serializer
->serialize($entity_translated, $this->format, $context);
$yaml_parsed['_translations'][$langcode] = Yaml::decode($normalized_entity_translations);
}
}
}
return Yaml::encode($yaml_parsed);
}
public function getFormat() {
return $this->format;
}
public function getSerializer() {
return $this->serializer;
}
}