View source
<?php
namespace Drupal\commerce_demo;
use Drupal\commerce_product\Entity\ProductAttributeValueInterface;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\taxonomy\TermInterface;
class ContentExporter {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function exportAll($entity_type_id, $bundle = '') {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if (!$entity_type
->entityClassImplements(ContentEntityInterface::class)) {
throw new \InvalidArgumentException(sprintf('The %s entity type is not a content entity type.', $entity_type_id));
}
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$query = $storage
->getQuery();
if ($bundle_key = $entity_type
->getKey('bundle')) {
$query
->condition($bundle_key, $bundle);
}
if ($entity_type_id == 'taxonomy_term') {
$query
->sort('depth_level', 'ASC');
$query
->sort('name', 'ASC');
}
$ids = $query
->execute();
if (!$ids) {
return [];
}
$export = [];
$entities = $storage
->loadMultiple($ids);
foreach ($entities as $entity) {
$export[$entity
->uuid()] = $this
->export($entity);
unset($export[$entity
->uuid()]['uuid']);
}
return $export;
}
public function export(ContentEntityInterface $entity) {
$id_key = $entity
->getEntityType()
->getKey('id');
$skip_fields = [
$id_key,
'langcode',
'default_langcode',
'uid',
'created',
'changed',
];
$export = [];
foreach ($entity
->getFields() as $field_name => $items) {
if (in_array($field_name, $skip_fields)) {
continue;
}
$items
->filterEmptyItems();
if ($items
->isEmpty()) {
continue;
}
$storage_definition = $items
->getFieldDefinition()
->getFieldStorageDefinition();
$list = $items
->getValue();
foreach ($list as $delta => $item) {
if ($storage_definition
->getType() == 'entity_reference') {
$target_entity_type_id = $storage_definition
->getSetting('target_type');
$target_entity_type = $this->entityTypeManager
->getDefinition($target_entity_type_id);
if ($target_entity_type
->entityClassImplements(ContentEntityInterface::class)) {
$item['target_id'] = $this
->mapToUuid($target_entity_type_id, $item['target_id']);
}
}
elseif ($storage_definition
->getType() == 'image') {
$file = $this->entityTypeManager
->getStorage('file')
->load($item['target_id']);
$item['filename'] = $file
->getFilename();
unset($item['target_id']);
unset($item['height']);
unset($item['width']);
$item = array_filter($item);
}
elseif ($storage_definition
->getType() == 'path') {
$item = array_intersect_key($item, [
'alias' => 'alias',
]);
}
$main_property_name = $storage_definition
->getMainPropertyName();
if ($main_property_name && isset($item[$main_property_name]) && count($item) === 1) {
$item = $item[$main_property_name];
}
$list[$delta] = $item;
}
if ($storage_definition
->getCardinality() === 1) {
$list = reset($list);
}
if (!empty($list)) {
$export[$field_name] = $list;
}
}
$entity_type_id = $entity
->getEntityTypeId();
if (substr($entity_type_id, 0, 9) == 'commerce_') {
$export = $this
->processCommerce($export, $entity);
}
if ($entity_type_id == 'commerce_product') {
$export = $this
->processProduct($export, $entity);
}
elseif ($entity_type_id == 'commerce_product_variation') {
$export = $this
->processVariation($export, $entity);
}
elseif ($entity_type_id == 'commerce_product_attribute_value') {
$export = $this
->processAttributeValue($export, $entity);
}
elseif ($entity_type_id == 'commerce_promotion') {
$export = $this
->processPromotion($export, $entity);
}
elseif ($entity_type_id == 'taxonomy_term') {
$export = $this
->processTerm($export, $entity);
}
return $export;
}
protected function processCommerce(array $export, ContentEntityInterface $entity) {
unset($export['stores']);
return $export;
}
protected function processProduct(array $export, ProductInterface $product) {
$variations = [];
foreach ($product
->getVariations() as $variation) {
$variations[$variation
->uuid()] = $this
->export($variation);
unset($variations[$variation
->uuid()]['uuid']);
}
$export['variations'] = $variations;
return $export;
}
protected function processVariation(array $export, ProductVariationInterface $variation) {
unset($export['product_id']);
return $export;
}
protected function processAttributeValue(array $export, ProductAttributeValueInterface $attribute_value) {
unset($export['weight']);
return $export;
}
protected function processPromotion(array $export, PromotionInterface $promotion) {
$coupons = [];
foreach ($promotion
->getCoupons() as $coupon) {
$coupons[$coupon
->uuid()] = $this
->export($coupon);
unset($coupons[$coupon
->uuid()]['uuid']);
}
$export['coupons'] = $coupons;
return $export;
}
protected function processTerm(array $export, TermInterface $term) {
$term_storage = $this->entityTypeManager
->getStorage('taxonomy_term');
if ($parents = $term_storage
->loadParents($term
->id())) {
$parent_ids = array_keys($parents);
$parent_ids = array_map(function ($parent_id) {
return $this
->mapToUuid('taxonomy_term', $parent_id);
}, $parent_ids);
$export = [
'parent' => $parent_ids,
] + $export;
}
return $export;
}
protected function mapToUuid($entity_type_id, $entity_id) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity = $storage
->load($entity_id);
return $entity ? $entity
->uuid() : NULL;
}
}