View source
<?php
namespace Drupal\config_partial_export\Form;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\StorageComparer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\State\StateInterface;
class ConfigPartialExportForm extends FormBase {
protected $activeStorage;
protected $snapshotStorage;
protected $configManager;
protected $fileSystem;
protected $state;
public function __construct(StorageInterface $active_storage, StorageInterface $snapshot_storage, ConfigManagerInterface $config_manager, FileSystemInterface $file_system, StateInterface $state) {
$this->activeStorage = $active_storage;
$this->snapshotStorage = $snapshot_storage;
$this->configManager = $config_manager;
$this->fileSystem = $file_system;
$this->state = $state;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.storage'), $container
->get('config.storage.snapshot'), $container
->get('config.manager'), $container
->get('file_system'), $container
->get('state'));
}
public function getFormId() {
return 'config_partial_export_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$snapshot_comparer = new StorageComparer($this->activeStorage, $this->snapshotStorage, $this->configManager);
$change_list = [];
if ($snapshot_comparer
->createChangelist()
->hasChanges()) {
$this
->messenger()
->addWarning($this
->t('Your current configuration has changed.'));
foreach ($snapshot_comparer
->getAllCollectionNames() as $collection) {
foreach ($snapshot_comparer
->getChangelist(NULL, $collection) as $config_names) {
if (empty($config_names)) {
continue;
}
foreach ($config_names as $config_name) {
$change_list[$config_name]['name'] = $config_name;
}
}
}
}
if (empty($change_list)) {
$user_input = $form_state
->getUserInput();
if (isset($user_input['change_list'])) {
$change_list = $user_input['change_list'];
}
}
ksort($change_list);
$form['change_list'] = [
'#type' => 'tableselect',
'#header' => [
'name' => $this
->t('Name'),
],
'#options' => $change_list,
];
$form['description'] = [
'#markup' => '<p><b>' . $this
->t('Use the export button to download the selected files listed above.') . '</b></p>',
];
$form['addSystemSiteInfo'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add system.site info'),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Export'),
];
$last_selection = $this->state
->get('config_partial_export_form');
$current_user_id = $this
->currentUser()
->id();
if (!empty($last_selection[$current_user_id])) {
$current_user_last_selection = $last_selection[$current_user_id];
$form['change_list']['#default_value'] = $current_user_last_selection['status_checkboxes_all'];
$form['addSystemSiteInfo']['#default_value'] = $current_user_last_selection['status_checkbox_system'];
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$count = 0;
if (!empty($user_input)) {
foreach ($user_input['change_list'] as $change_item) {
if ($change_item) {
$count++;
}
}
}
if ((empty($user_input['change_list']) || !$count) && empty($user_input['addSystemSiteInfo'])) {
$form_state
->setErrorByName('', $this
->t('No items selected.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$change_list = $user_input['change_list'] ?: [];
$add_system_site_info = $user_input['addSystemSiteInfo'];
$change_list_booleans = [];
foreach ($change_list as $key => $value) {
if ($value) {
$change_list_booleans[$key] = TRUE;
}
}
$last_selection = $this->state
->get('config_partial_export_form');
$last_selection[$this
->currentUser()
->id()] = [
'status_checkboxes_all' => $change_list_booleans,
'status_checkbox_system' => (bool) $add_system_site_info,
];
$this->state
->set('config_partial_export_form', $last_selection);
$this
->createArchive(array_filter($change_list), $add_system_site_info);
$form_state
->setRedirect('config_partial.export_partial_download');
}
public function createArchive(array $change_list, $add_system_site_info = FALSE) {
$this->fileSystem
->delete($this->fileSystem
->getTempDirectory() . '/config_partial.tar.gz');
$archiver = new ArchiveTar($this->fileSystem
->getTempDirectory() . '/config_partial.tar.gz', 'gz');
if ($add_system_site_info && !in_array('system.site', $change_list)) {
$change_list[] = 'system.site';
}
foreach ($change_list as $name) {
$yaml = Yaml::encode($this->configManager
->getConfigFactory()
->get($name)
->getRawData());
$archiver
->addString("{$name}.yml", $yaml);
}
}
}