Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
default_content
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
default_content
Commits
ecb956f0
Commit
ecb956f0
authored
Sep 9, 2014
by
Daniel Wehner
Browse files
Options
Downloads
Patches
Plain Diff
provide support to export entities with all its references
parent
1406ebc1
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
drush/default_content.drush.inc
+38
-3
38 additions, 3 deletions
drush/default_content.drush.inc
src/DefaultContentManager.php
+55
-0
55 additions, 0 deletions
src/DefaultContentManager.php
src/DefaultContentManagerInterface.php
+13
-0
13 additions, 0 deletions
src/DefaultContentManagerInterface.php
with
106 additions
and
3 deletions
drush/default_content.drush.inc
+
38
−
3
View file @
ecb956f0
...
...
@@ -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
);
}
}
}
This diff is collapsed.
Click to expand it.
src/DefaultContentManager.php
+
55
−
0
View file @
ecb956f0
...
...
@@ -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
*
...
...
This diff is collapsed.
Click to expand it.
src/DefaultContentManagerInterface.php
+
13
−
0
View file @
ecb956f0
...
...
@@ -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
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment