ContentExportMultiple.php in Content Synchronization 8.2
File
src/Form/ContentExportMultiple.php
View source
<?php
namespace Drupal\content_sync\Form;
use Drupal\content_sync\ContentSyncManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ContentExportMultiple extends ConfirmFormBase {
use ContentExportTrait;
protected $entityTypeManager;
protected $tempStoreFactory;
protected $contentSyncManager;
protected $contentSyncUIToolbox;
protected $entityList = [];
protected $formats;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $manager, ContentSyncManagerInterface $content_sync_manager, array $formats, FileSystemInterface $file_system) {
$this->tempStoreFactory = $temp_store_factory;
$this->entityTypeManager = $manager;
$this->contentSyncManager = $content_sync_manager;
$this->formats = $formats;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'), $container
->get('content_sync.manager'), $container
->getParameter('serializer.formats'), $container
->get('file_system'));
}
public function getFormId() {
return 'content_sync_export_multiple_confirm';
}
public function getQuestion() {
return $this
->formatPlural(count($this->entityList), 'Are you sure you want to export this item?', 'Are you sure you want to export these items?');
}
public function getCancelUrl() {
return new Url('system.admin_content');
}
public function getConfirmText() {
return t('Export');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->entityList = $this->tempStoreFactory
->get('content_sync_ui_multiple_confirm')
->get($this
->currentUser()
->id());
if (empty($this->entityList)) {
return new RedirectResponse($this
->getCancelUrl()
->setAbsolute()
->toString());
}
$items = [];
foreach ($this->entityList as $uuid => $entity_info) {
$storage = $this->entityTypeManager
->getStorage($entity_info['entity_type']);
$entity = $storage
->load($entity_info['entity_id']);
if (!empty($entity)) {
$items[$uuid] = $entity
->label();
}
}
$form['content_list'] = [
'#theme' => 'item_list',
'#title' => 'Content List.',
'#items' => $items,
];
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm') && !empty($this->entityList)) {
$this->fileSystem
->delete($this
->getTempFile());
$entities_list = [];
foreach ($this->entityList as $entity_info) {
$entities_list[] = [
'entity_type' => $entity_info['entity_type'],
'entity_id' => $entity_info['entity_id'],
];
}
if (!empty($entities_list)) {
$batch = $this
->generateBatch($entities_list);
batch_set($batch);
}
}
else {
$form_state
->setRedirect('system.admin_content');
}
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
protected function getContentExporter() {
return $this->contentSyncManager
->getContentExporter();
}
protected function getExportLogger() {
return $this
->logger('content_sync');
}
}