You are here

public function ContentExporter::exportAll in Commerce Demo 8

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

Exports all entities of the given type, restricted by bundle.

Parameters

string $entity_type_id: The entity type ID.

string $bundle: The bundle.

Return value

array The exported entities, keyed by UUID.

File

src/ContentExporter.php, line 49

Class

ContentExporter
Defines the content exporter.

Namespace

Drupal\commerce_demo

Code

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);
  }

  // Root terms need to be imported first.
  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);

    // The array is keyed by UUID, no need to have it in the export too.
    unset($export[$entity
      ->uuid()]['uuid']);
  }
  return $export;
}