public function Exporter::exportContentWithReferences in Default Content for D8 2.0.x
Same name and namespace in other branches
- 8 src/Exporter.php \Drupal\default_content\Exporter::exportContentWithReferences()
Exports a single entity and all its referenced entity.
Parameters
string $entity_type_id: The entity type ID.
mixed $entity_id: The entity ID to export.
string|null $folder: (optional) A folder to write the exported entities into, grouped by entity type. File entities also export their files into the same folder.
Return value
string[][] The serialized entities keyed by entity type and UUID.
Overrides ExporterInterface::exportContentWithReferences
File
- src/
Exporter.php, line 129
Class
- Exporter
- A service for handling import of default content.
Namespace
Drupal\default_contentCode
public function exportContentWithReferences($entity_type_id, $entity_id, $folder = NULL) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity = $storage
->load($entity_id);
if (!$entity) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" does not exist', $entity_type_id, $entity_id));
}
if (!$entity instanceof ContentEntityInterface) {
throw new \InvalidArgumentException(sprintf('Entity "%s" with ID "%s" is not a content entity', $entity_type_id, $entity_id));
}
$entities = [
$entity
->uuid() => $entity,
];
$entities = $this
->getEntityReferencesRecursive($entity, 0, $entities);
// Serialize all entities and key them by entity TYPE and uuid.
$serialized_entities_per_type = [];
foreach ($entities as $entity) {
$normalized = $this->contentEntityNormalizer
->normalize($entity);
$encoded = Yaml::encode($normalized);
$serialized_entities_per_type[$entity
->getEntityTypeId()][$entity
->uuid()] = $encoded;
if ($folder) {
$this->contentFileStorage
->writeEntity($folder, $encoded, $entity);
}
}
return $serialized_entities_per_type;
}