Skip to content
Snippets Groups Projects
Commit ecb956f0 authored by Daniel Wehner's avatar Daniel Wehner
Browse files

provide support to export entities with all its references

parent 1406ebc1
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,19 @@ function default_content_drush_command() {
'options' => [
'file' => dt('Write out the exported content to a file instead of stdout'),
],
'aliases' => array('dce'),
'required-arguments' => 2,
];
$items['default-content-export-references'] = [
'description' => dt('Exports an entity and all its referenced entities.'),
'arguments' => [
'entity_type' => dt('The entity type to export.'),
'entity_id' => dt('The ID of the entity to export.'),
],
'options' => [
'folder' => dt('Folder to export to, entities are grouped by entity type into directories.'),
],
'aliases' => array('dcer'),
'required-arguments' => 2,
];
......@@ -31,9 +44,6 @@ function default_content_drush_command() {
* The entity type ID.
* @param $entity_id
* The entity ID to export.
*
* @return string
* The rendered export as hal.
*/
function drush_default_content_export($entity_type_id, $entity_id) {
/** @var \Drupal\default_content\DefaultContentManagerInterface $manager */
......@@ -47,3 +57,28 @@ function drush_default_content_export($entity_type_id, $entity_id) {
drush_print($export);
}
}
/**
* Exports a piece of content and all its referenced entities.
*
* @param string $entity_type_id
* The entity type ID.
* @param $entity_id
* The entity ID to export.
*/
function drush_default_content_export_references($entity_type_id, $entity_id) {
/** @var \Drupal\default_content\DefaultContentManagerInterface $manager */
$manager = \Drupal::service('default_content.manager');
$folder = drush_get_option('folder', '.');
$serialized_by_type = $manager->exportContentWithReferences($entity_type_id, $entity_id);
foreach ($serialized_by_type as $entity_type => $serialized_entities) {
// Ensure that the folder per entity type exists.
$entity_type_folder = "$folder/$entity_type";
file_prepare_directory($entity_type_folder, FILE_CREATE_DIRECTORY);
foreach ($serialized_entities as $entity_id => $serialized_entity) {
file_put_contents($entity_type_folder . '/' . $entity_id . '.json', $serialized_entity);
}
}
}
......@@ -7,7 +7,11 @@
namespace Drupal\default_content;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Session\AccountInterface;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Gliph\Graph\DirectedAdjacencyList;
......@@ -165,6 +169,57 @@ class DefaultContentManager implements DefaultContentManagerInterface {
return $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
}
/**
* {@inheritdoc}
*/
public function exportContentWithReferences($entity_type_id, $entity_id) {
$storage = $this->entityManager->getStorage($entity_type_id);
$entity = $storage->load($entity_id);
/** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
$entities = [$entity];
$entities = array_merge($entities, $this->getEntityReferencesRecursive($entity));
$serialized_entities_per_type = [];
// Serialize all entities and key them by entity TYPE and uuid.
foreach ($entities as $entity) {
$serialized_entities_per_type[$entity->getEntityTypeId()][$entity->uuid()] = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
}
return $serialized_entities_per_type;
}
/**
* Returns all referenced entities of an entity.
*
* This method is also recursive to support usecases like a node -> media
* -> file.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
* @param int $depth
* Guard against infinite recursion.
*
* @return \Drupal\Core\Entity\EntityInterface[]
*/
protected function getEntityReferencesRecursive(ContentEntityInterface $entity, $depth = 0) {
$entity_dependencies = $entity->referencedEntities();
foreach ($entity_dependencies as $id => $dependent_entity) {
// Config entities should not be exported but rather provided by default
// config.
if ($dependent_entity instanceof ConfigEntityInterface) {
unset($entity_dependencies[$id]);
}
$entity_dependencies = array_merge($entity_dependencies, $this->getEntityReferencesRecursive($dependent_entity, $depth + 1));
}
// Build in some support against infinite recursion.
if ($depth > 5) {
return $entity_dependencies;
}
return array_unique($entity_dependencies);
}
/**
* Utility to get a default content scanner
*
......
......@@ -44,4 +44,17 @@ interface DefaultContentManagerInterface {
*/
public function exportContent($entity_type_id, $entity_id);
/**
* Exports a single entity and all its referenced entity.
*
* @param string $entity_type_id
* The entity type ID.
* @param $entity_id
* The entity ID to export.
*
* @return string[][]
* The serialized entities keyed by entity type and UUID.
*/
public function exportContentWithReferences($entity_type_id, $entity_id);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment