ContentSyncExport.php in Content Synchronization 8
File
src/Content/ContentSyncExport.php
View source
<?php
namespace Drupal\content_sync\Content;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\ContentEntityType;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentSyncExport {
protected $activeStorage;
public function __construct(StorageInterface $active_storage) {
$this->activeStorage = $active_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content.storage.active'));
}
public function processContentSyncSnapshot($files, &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_number'] = 0;
$context['sandbox']['max'] = count($files);
}
$entity_type = $files[$context['sandbox']['progress']]['entity_type'];
$entity_bundle = $files[$context['sandbox']['progress']]['entity_bundle'];
$entity_id = $files[$context['sandbox']['progress']]['entity_id'];
$entityTypeManager = \Drupal::entityTypeManager();
$instances = $entityTypeManager
->getDefinitions();
if (!(isset($instances[$entity_type]) && $instances[$entity_type] instanceof ContentEntityType)) {
$context['results']['errors'][] = t('Entity type does not exist or it is not a content instance.') . $entity_type;
}
else {
$entityTypeManager = \Drupal::entityTypeManager();
$entityFieldManager = \Drupal::service('entity_field.manager');
$fields = array_filter($entityFieldManager
->getFieldDefinitions($entity_type, $entity_bundle), function ($field_definition) {
return $field_definition;
});
$entity_elements = [];
foreach ($fields as $fieldID => $field) {
$entity_elements[$field
->getName()] = $field
->getName();
}
$properties = $entityTypeManager
->getDefinitions()[$entity_type]
->getKeys();
$entity_data = $entityTypeManager
->getStorage($entity_type)
->load($entity_id);
$entity = [];
$entity['entity_type'] = $entity_type;
$entity['bundle'] = $entity_bundle;
$site_config = \Drupal::config('system.site');
$site_uuid_source = $site_config
->get('uuid');
$entity['site_uuid'] = $site_uuid_source;
unset($entity_elements[$properties['id']]);
unset($entity_elements[$properties['bundle']]);
unset($entity_elements['vid']);
$entity_elements = array_filter($entity_elements);
foreach ($entity_elements as $elementID => $element) {
if ($element == 'parent') {
$parent = $entityTypeManager
->getStorage($entity_type)
->loadParents($entity_id);
$parent = reset($parent);
if (!empty($parent)) {
$entity['values'][0][$element] = $parent
->uuid();
}
}
else {
$entity['values'][0][$element] = $entity_data
->get($element)
->getValue();
}
$element_type = $entity_data
->get($element)
->getFieldDefinition()
->getType();
if ($element_type == "entity_reference" || $element_type == "image" || $element_type == "file") {
if ($entity_data
->get($element)->entity) {
$reference_type = $entity_data
->get($element)->entity
->getEntityType()
->id();
foreach ($entity_data
->get($element)
->getValue() as $er_key => $er_val) {
$entity['values'][0][$element][$er_key]['target_id'] = $entityTypeManager
->getStorage($reference_type)
->load($er_val['target_id'])
->uuid();
}
}
}
}
if ($entity_type != "file") {
$entity['values'][0]['path'] = "/" . $entity_data
->toUrl()
->getInternalPath();
}
$lang_default = $entity['values'][0]['langcode'][0]['value'];
foreach ($entity_data
->getTranslationLanguages() as $langcode => $language) {
$c = 0;
if ($entity_data
->hasTranslation($langcode)) {
$entity_data_translation = $entity_data
->getTranslation($langcode);
if ($langcode != $lang_default) {
foreach ($entity_elements as $elementID => $element) {
if ($fields[$elementID]
->isTranslatable() == TRUE) {
$entity['values'][0]['translations'][$c][$element] = $entity_data_translation
->get($element)
->getValue();
$element_type = $entity_data_translation
->get($element)
->getFieldDefinition()
->getType();
if ($element_type == "entity_reference" || $element_type == "image" || $element_type == "file") {
if ($entity_data_translation
->get($element)->entity) {
$reference_type = $entity_data_translation
->get($element)->entity
->getEntityType()
->id();
foreach ($entity_data_translation
->get($element)
->getValue() as $er_key => $er_val) {
$entity['values'][0]['translations'][$c][$element][$er_key]['target_id'] = $entityTypeManager
->getStorage($reference_type)
->load($er_val['target_id'])
->uuid();
}
}
}
}
}
$c++;
}
}
}
$name = $entity_type . "." . $entity_bundle . "." . $entity['values'][0]['uuid'][0]['value'];
$this->activeStorage
->write($name, $entity);
$context['message'] = $name;
$context['results'][] = $name;
}
$context['sandbox']['progress']++;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
}