BulkNodeExport.php in Node export 8
File
src/Form/BulkNodeExport.php
View source
<?php
namespace Drupal\node_Export\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\node_export\NodeExport;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
class BulkNodeExport extends FormBase {
protected $step = 1;
protected $userInput = [];
protected $tempStoreFactory;
private $sessionManager;
private $currentUser;
protected $routeBuilder;
public function __construct(PrivateTempStoreFactory $temp_store_factory, SessionManagerInterface $session_manager, AccountInterface $current_user, RouteBuilderInterface $route_builder) {
$this->tempStoreFactory = $temp_store_factory;
$this->sessionManager = $session_manager;
$this->currentUser = $current_user;
$this->routeBuilder = $route_builder;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('session_manager'), $container
->get('current_user'), $container
->get('router.builder'));
}
public function getFormId() {
return 'bulk_export_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this
->messenger()
->addWarning($this
->t('This module is experiemental. PLEASE do not use on production databases without prior testing and a complete database dump.'));
$this->userInput['entities'] = $this->tempStoreFactory
->get('node_export_ids')
->get($this->currentUser
->id());
$json = NodeExport::export(array_keys($this->userInput['entities']), 'json', FALSE);
$form['export_code'] = [
'#type' => 'textarea',
'#value' => $json,
'#title' => $this
->t('Node Export Code is :'),
'#rows' => '15',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Download'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$data = $form_state
->getValue('export_code');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename('node.json'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('node.json'));
print $data;
exit;
}
}