From 0d5e8edd501a545d6e0b903931be0276bc3d8c17 Mon Sep 17 00:00:00 2001
From: Antonio Ospite <ao2@ao2.it>
Date: Sat, 25 Feb 2017 15:48:21 +0100
Subject: [PATCH 1/2] skip-exporting-core-users-by-default-2815051

---
 drush/default_content.drush.inc        | 5 ++++-
 src/DefaultContentManager.php          | 9 +++++++--
 src/DefaultContentManagerInterface.php | 4 +++-
 3 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/drush/default_content.drush.inc b/drush/default_content.drush.inc
index 65375f9..f3da827 100644
--- a/drush/default_content.drush.inc
+++ b/drush/default_content.drush.inc
@@ -30,6 +30,7 @@ function default_content_drush_command() {
     ],
     'options' => [
       'folder' => dt('Folder to export to, entities are grouped by entity type into directories.'),
+      'include-core-users' => dt('Export user entities created by core'),
     ],
     'aliases' => ['dcer'],
     'required-arguments' => 1,
@@ -80,6 +81,8 @@ function drush_default_content_export_references($entity_type_id, $entity_id = N
   $manager = \Drupal::service('default_content.manager');
 
   $folder = drush_get_option('folder', '.');
+  $include_core_users = drush_get_option('include-core-users', FALSE);
+
   if (is_null($entity_id)) {
     $entities = \Drupal::entityQuery($entity_type_id)->execute();
   }
@@ -88,7 +91,7 @@ function drush_default_content_export_references($entity_type_id, $entity_id = N
   }
   // @todo Add paging.
   foreach ($entities as $entity_id) {
-    $serialized_by_type = $manager->exportContentWithReferences($entity_type_id, $entity_id);
+    $serialized_by_type = $manager->exportContentWithReferences($entity_type_id, $entity_id, $include_core_users);
     $manager->writeDefaultContent($serialized_by_type, $folder);
   }
 }
diff --git a/src/DefaultContentManager.php b/src/DefaultContentManager.php
index 40acca2..2521737 100644
--- a/src/DefaultContentManager.php
+++ b/src/DefaultContentManager.php
@@ -252,7 +252,7 @@ class DefaultContentManager implements DefaultContentManagerInterface {
   /**
    * {@inheritdoc}
    */
-  public function exportContentWithReferences($entity_type_id, $entity_id) {
+  public function exportContentWithReferences($entity_type_id, $entity_id, $include_core_users = FALSE) {
     $storage = $this->entityManager->getStorage($entity_type_id);
     $entity = $storage->load($entity_id);
 
@@ -268,9 +268,14 @@ class DefaultContentManager implements DefaultContentManagerInterface {
 
     $serialized_entities_per_type = [];
     $this->linkManager->setLinkDomain(static::LINK_DOMAIN);
+
     // 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]);
+      // Skip core users, unless it's indicated that they should be exported.
+      $skip = !$include_core_users && $entity->getEntityTypeId() == 'user' && in_array($entity->id(), [0, 1]);
+      if (!$skip) {
+        $serialized_entities_per_type[$entity->getEntityTypeId()][$entity->uuid()] = $this->serializer->serialize($entity, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
+      }
     }
     $this->linkManager->setLinkDomain(FALSE);
 
diff --git a/src/DefaultContentManagerInterface.php b/src/DefaultContentManagerInterface.php
index fdf67a9..76dce91 100644
--- a/src/DefaultContentManagerInterface.php
+++ b/src/DefaultContentManagerInterface.php
@@ -46,11 +46,13 @@ interface DefaultContentManagerInterface {
    *   The entity type ID.
    * @param mixed $entity_id
    *   The entity ID to export.
+   * @param bool $skip_core_users
+   *   Whether or not to skip superuser and anonymous accounts when exporting.
    *
    * @return string[][]
    *   The serialized entities keyed by entity type and UUID.
    */
-  public function exportContentWithReferences($entity_type_id, $entity_id);
+  public function exportContentWithReferences($entity_type_id, $entity_id, $include_core_users = FALSE);
 
   /**
    * Exports all of the content defined in a module's info file.
-- 
2.11.0

