ContentExportForm.php in Content Synchronization 8.2
File
src/Form/ContentExportForm.php
View source
<?php
namespace Drupal\content_sync\Form;
use Drupal\content_sync\ContentSyncManagerInterface;
use Drupal\content_sync\Exporter\ContentExporterInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentExportForm extends FormBase {
use ContentExportTrait;
protected $entityTypeManager;
protected $contentExporter;
protected $contentSyncManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentExporterInterface $content_exporter, ContentSyncManagerInterface $content_sync_manager, FileSystemInterface $file_system) {
$this->entityTypeManager = $entity_type_manager;
$this->contentExporter = $content_exporter;
$this->contentSyncManager = $content_sync_manager;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('content_sync.exporter'), $container
->get('content_sync.manager'), $container
->get('file_system'));
}
public function getFormId() {
return 'content_export_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Export'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->fileSystem
->delete($this
->getTempFile());
$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)) {
$serializer_context['export_type'] = 'tar';
$serializer_context['include_files'] = 'folder';
$batch = $this
->generateExportBatch($entities_list, $serializer_context);
batch_set($batch);
}
}
public function snapshot() {
$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)) {
$serializer_context['export_type'] = 'snapshot';
$batch = $this
->generateExportBatch($entities_list, $serializer_context);
batch_set($batch);
}
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
protected function getContentExporter() {
return $this->contentExporter;
}
protected function getExportLogger() {
return $this
->logger('content_sync');
}
}