You are here

public function ContentExporter::export in Commerce Demo 8

Same name and namespace in other branches
  1. 8.2 src/ContentExporter.php \Drupal\commerce_demo\ContentExporter::export()

Exports the given entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

Return value

array The export array.

3 calls to ContentExporter::export()
ContentExporter::exportAll in src/ContentExporter.php
Exports all entities of the given type, restricted by bundle.
ContentExporter::processProduct in src/ContentExporter.php
Processes the exported product.
ContentExporter::processPromotion in src/ContentExporter.php
Processes the exported promotion.

File

src/ContentExporter.php, line 91

Class

ContentExporter
Defines the content exporter.

Namespace

Drupal\commerce_demo

Code

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)) {

          // Map entity_reference IDs to UUIDs.
          $item['target_id'] = $this
            ->mapToUuid($target_entity_type_id, $item['target_id']);
        }
      }
      elseif ($storage_definition
        ->getType() == 'image') {

        // Replace the 'target_id' with the filename.

        /** @var \Drupal\file\FileInterface $file */
        $file = $this->entityTypeManager
          ->getStorage('file')
          ->load($item['target_id']);
        $item['filename'] = $file
          ->getFilename();
        unset($item['target_id']);

        // Remove calculated values.
        unset($item['height']);
        unset($item['width']);

        // Remove empty keys.
        $item = array_filter($item);
      }
      elseif ($storage_definition
        ->getType() == 'path') {

        // Remove calculated values.
        $item = array_intersect_key($item, [
          'alias' => 'alias',
        ]);
      }

      // Simplify items with a single key (such as "value").
      $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;
    }

    // Remove the wrapping array if the field is single-valued.
    if ($storage_definition
      ->getCardinality() === 1) {
      $list = reset($list);
    }
    if (!empty($list)) {
      $export[$field_name] = $list;
    }
  }
  $entity_type_id = $entity
    ->getEntityTypeId();

  // Perform generic processing.
  if (substr($entity_type_id, 0, 9) == 'commerce_') {
    $export = $this
      ->processCommerce($export, $entity);
  }

  // Process by entity type ID.
  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;
}