Skip to content

Commit

Permalink
Issue #2978882 by kfritsche, klimp, luis_mejia, david4lim, dabito, el…
Browse files Browse the repository at this point in the history
…1_1el, Ishani.addweb, Spudley, nimoatwoodway, steveoriol, sccherry, snehal.addweb, jigish.addweb, Ronak.addweb, webfaqtory, Prabhu.shan, Prashant.c, Blanca.Esqueda: Fixed miscellaneous Install and Export content issues
  • Loading branch information
Blanca.Esqueda authored and Blanca.Esqueda committed Feb 24, 2019
1 parent 694b8e6 commit f454d49
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 70 deletions.
57 changes: 3 additions & 54 deletions content_sync.install
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
<?php

/**
* @file
* Install, update and uninstall functions for the content_sync module.
*/

use Drupal\Core\Entity\ContentEntityType;

/**
* Implements hook_install().
*/
function content_sync_install(){
//TODO - Move this and the batch to a class
//Entity types manager
$entityTypeManager = \Drupal::entityTypeManager();
$entityBundles = \Drupal::service("entity_type.bundle.info");
//Set batch operations by entity type/bundle
$operations = [];
$operations[] = ['generateSiteUUIDFile', [0=>'snapshot']];
$entity_type_definitions = $entityTypeManager->getDefinitions();
foreach ($entity_type_definitions as $entity_type => $definition) {
if ($definition instanceof ContentEntityType) {
$entity_bundles = $entityBundles->getBundleInfo($entity_type);
foreach ($entity_bundles as $entity_bundle => $bundle) {
//Get BundleKey
$bundleKey = \Drupal::entityTypeManager()->getStorage($entity_type)->getEntityType()->getKey('bundle');
if (!empty($bundleKey)) {
// Load entities by their property values.
$entities = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadByProperties(array($bundleKey => $entity_bundle));
}else{
$entities = \Drupal::entityTypeManager()
->getStorage($entity_type)
->loadMultiple();
}
$entity = [];
foreach($entities as $entity_id => $entity_obj) {
$entity['values'][] = [
'entity_type' => $entity_type,
'entity_bundle' => $entity_bundle,
'entity_id' => $entity_id
];
}
if(!empty($entity)) {
$operations[] = ['processContentSyncSnapshot', $entity];
}
}
}
}
if(empty($operations)){
$operations[] = ['processContentSyncSnapshot', [0=>0] ];
}
//Set Batch
$batch = [
'operations' => $operations,
'title' => t('Content Snapshot'),
'init_message' => t('Starting content snapshot.'),
'progress_message' => t('Completed @current step of @total.'),
'error_message' => t('Content sync snapshot has encountered an error.'),
'file' => drupal_get_path('module', 'content_sync') . '/content_sync.batch.inc',
];
batch_set($batch);
//Create the content snapshot.
$cs_snapshoot = Drupal::service('content_sync.snaphoshot');
$cs_snapshoot->snapshot();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions content_sync.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ services:
tags:
- { name: logger }
- { name: backend_overridable }
content_sync.snaphoshot:
class: Drupal\content_sync\Form\ContentExportForm
arguments: ['@entity_type.manager','@content_sync.exporter']
content.storage.staging:
class: Drupal\Core\Config\FileStorage
factory: Drupal\content_sync\Content\ContentFileStorageFactory::getSync
Expand Down
24 changes: 23 additions & 1 deletion src/Form/ContentExportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
batch_set($batch);
}
}
public function snapshot() {
//Set batch operations by entity type/bundle
$entities_list = [];
$entity_type_definitions = $this->entityTypeManager->getDefinitions();
foreach ($entity_type_definitions as $entity_type => $definition) {
$reflection = new \ReflectionClass($definition->getClass());
if ($reflection->implementsInterface(ContentEntityInterface::class)) {
$entities = $this->entityTypeManager->getStorage($entity_type)
->getQuery()
->execute();
foreach ($entities as $entity_id) {
$entities_list[] = [
'entity_type' => $entity_type,
'entity_id' => $entity_id,
];
}
}
}
if (!empty($entities_list)) {
$batch = $this->generateBatch($entities_list, 'snapshot');
batch_set($batch);
}
}

/**
* @{@inheritdoc}
Expand All @@ -112,4 +135,3 @@ protected function getExportLogger() {
}

}

41 changes: 27 additions & 14 deletions src/Form/ContentExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,33 @@ trait ContentExportTrait {
/**
* @param $entities
*
* @param $export_type
* files => YAML files.
* snapshot => cs_db_snapshot table.
*
* @return array
*/
public function generateBatch($entities) {
public function generateBatch($entities, $export_type = 'files') {
//Set batch operations by entity type/bundle
$operations = [];
$operations[] = [[$this, 'generateSiteUUIDFile'], [0 => 0]];
$operations[] = [[$this, 'generateSiteUUIDFile'], [0 => $export_type]];
foreach ($entities as $entity) {
$entity_to_export = [];
$entity['export_type'] = $export_type;
$entity_to_export['values'][] = $entity;
$operations[] = [[$this, 'processContentExportFiles'], $entity_to_export];
}
if (empty($operations)) {
$operations[] = [[$this, 'processContentExportFiles'], [0 => 0]];
}
//Set Batch
$batch = [
'operations' => $operations,
'title' => $this->t('Exporting content'),
'init_message' => $this->t('Starting content export.'),
'progress_message' => $this->t('Completed @current step of @total.'),
'error_message' => $this->t('Content export has encountered an error.'),
'finished' => [$this,'finishContentExportBatch'],
];
if ($export_type == 'files') {
$batch['finished'] = [$this,'finishContentExportBatch'];
}
return $batch;
}

Expand All @@ -61,9 +65,11 @@ public function processContentExportFiles($files, &$context) {
$context['sandbox']['current_number'] = 0;
$context['sandbox']['max'] = count($files);
}

// Get submitted values
$entity_type = $files[$context['sandbox']['progress']]['entity_type'];
$entity_id = $files[$context['sandbox']['progress']]['entity_id'];
$export_type = $files[$context['sandbox']['progress']]['export_type'];

//Validate that it is a Content Entity
$instances = $this->getEntityTypeManager()->getDefinitions();
Expand All @@ -79,10 +85,17 @@ public function processContentExportFiles($files, &$context) {
->exportEntity($entity, $serializer_context);
// Create the name
$name = $entity_type . "." . $entity->bundle() . "." . $entity->uuid();
// Create the file.
$this->getArchiver()->addString("$name.yml", $exported_entity);
$context['message'] = $name;
$context['results'][] = $name;

if ($export_type == 'snapshot') {
//Save to cs_db_snapshot table.
$activeStorage = new DatabaseStorage(\Drupal::database(), 'cs_db_snapshot');
$activeStorage->write($name, Yaml::decode($exported_entity));
}else{
// Create the file.
$this->getArchiver()->addString("$name.yml", $exported_entity);
$context['message'] = $name;
$context['results'][] = $name;
}
}
$context['sandbox']['progress']++;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
Expand All @@ -108,14 +121,14 @@ public function generateSiteUUIDFile($data, &$context) {

// Set the name
$name = "site.uuid";
// Create the file.
$this->getArchiver()->addString("$name.yml", Yaml::encode($entity));

//Save to cs_db_snapshot if being called from installer.
if ($data == 'snapshot') {
// Insert Data
//Save to cs_db_snapshot table.
$activeStorage = new DatabaseStorage(\Drupal::database(), 'cs_db_snapshot');
$activeStorage->write($name, $entity);
}else{
// Create the file.
$this->getArchiver()->addString("$name.yml", Yaml::encode($entity));
}

$context['message'] = $name;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/ContentSingleImportForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ContentSingleImportForm extends FormBase {
protected $contentImporter;

/**
* ContentExportForm constructor.
* ContentImportForm constructor.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentImporterInterface $content_importer) {
$this->entityTypeManager = $entity_type_manager;
Expand Down

0 comments on commit f454d49

Please sign in to comment.