View source
<?php
namespace Drupal\config_installer\Form;
use Drupal\Core\Archiver\ArchiveTar;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\config_installer\Storage\SourceStorage;
include_once __DIR__ . '/../Storage/SourceStorage.php';
class SyncConfigureForm extends FormBase {
public function getFormId() {
return 'config_installer_sync_configure_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#title'] = $this
->t('Configure configuration import location');
$form['sync_directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('Synchronisation directory'),
'#default_value' => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
'#maxlength' => 255,
'#description' => $this
->t('Path to the config directory you wish to import, can be relative to document root or an absolute path.'),
'#required' => TRUE,
'#disabled' => !is_writable(\Drupal::service('site.path') . '/settings.php'),
];
$form['import_tarball'] = [
'#type' => 'file',
'#title' => $this
->t('Select your configuration export file'),
'#description' => $this
->t('If the sync directory is empty you can upload a configuration export file.'),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save and continue'),
'#weight' => 15,
'#button_type' => 'primary',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
global $config_directories;
global $install_state;
$file_upload = $this
->getRequest()->files
->get('files', NULL, TRUE);
$has_upload = FALSE;
if (!empty($file_upload['import_tarball']) && $file_upload['import_tarball']
->isValid()) {
$form_state
->setValue('import_tarball', $file_upload['import_tarball']
->getRealPath());
$has_upload = TRUE;
}
$sync_directory = $form_state
->getValue('sync_directory');
if ($sync_directory !== config_get_config_directory(CONFIG_SYNC_DIRECTORY)) {
$create_directory = !is_dir($sync_directory) || $has_upload;
if ($create_directory && !file_prepare_directory($sync_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
$form_state
->setErrorByName('sync_directory', t('The directory %directory could not be created or could not be made writable. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see the <a href="@handbook_url">online handbook</a>.', [
'%directory' => $sync_directory,
'@handbook_url' => 'http://drupal.org/server-permissions',
]));
}
}
if (!$has_upload && !$form_state
->hasAnyErrors()) {
$sync = new FileStorage($sync_directory);
if (count($sync
->listAll()) === 0) {
$form_state
->setErrorByName('sync_directory', $this
->t('No file upload provided and the sync directory is empty'));
}
}
if ($form_state
->hasAnyErrors()) {
return;
}
if ($sync_directory !== config_get_config_directory(CONFIG_SYNC_DIRECTORY)) {
$settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [
'value' => $sync_directory,
'required' => TRUE,
];
drupal_rewrite_settings($settings);
$config_directories[CONFIG_SYNC_DIRECTORY] = $sync_directory;
}
if ($tarball_path = $form_state
->getValue('import_tarball')) {
$sync = new FileStorage($sync_directory);
$sync
->deleteAll();
try {
$archiver = new ArchiveTar($tarball_path, 'gz');
$files = [];
$list = $archiver
->listContent();
if (is_array($list)) {
foreach ($list as $file) {
$files[] = $file['filename'];
}
$archiver
->extractList($files, config_get_config_directory(CONFIG_SYNC_DIRECTORY));
}
} catch (\Exception $e) {
$form_state
->setErrorByName('import_tarball', $this
->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', [
'@message' => $e
->getMessage(),
]));
}
drupal_unlink($tarball_path);
if (empty($files)) {
$form_state
->setErrorByName('import_tarball', $this
->t('The tar file contoins no files.'));
}
}
if ($form_state
->hasAnyErrors()) {
return;
}
$listing = new ExtensionDiscovery(\Drupal::root());
$listing
->setProfileDirectories([]);
$sync = new SourceStorage(\Drupal::service('config.storage.sync'), $listing
->scan('profile'));
$system_site = $sync
->read('system.site');
\Drupal::configFactory()
->getEditable('system.site')
->set('uuid', $system_site['uuid'])
->save();
if (version_compare(\Drupal::VERSION, '8.3', '>=')) {
\Drupal::configFactory()
->getEditable('core.extension')
->set('profile', _config_installer_get_original_install_profile())
->save();
}
$config_manager = \Drupal::service('config.manager');
$storage_comparer = new StorageComparer($sync, \Drupal::service('config.storage'), $config_manager);
$storage_comparer
->createChangelist();
$config_importer = new ConfigImporter($storage_comparer, \Drupal::service('event_dispatcher'), $config_manager, \Drupal::service('lock.persistent'), \Drupal::service('config.typed'), \Drupal::service('module_handler'), \Drupal::service('module_installer'), \Drupal::service('theme_handler'), \Drupal::service('string_translation'));
$install_state['parameters']['profile'] = _config_installer_get_original_install_profile();
try {
$config_importer
->validate();
} catch (ConfigImporterException $e) {
$error_message = [
'#type' => 'inline_template',
'#template' => '{% trans %}The configuration cannot be imported because it failed validation for the following reasons:{% endtrans%}{{ errors }}',
'#context' => [
'errors' => [
'#theme' => 'item_list',
'#items' => $config_importer
->getErrors(),
],
],
];
$field_name = $has_upload ? 'import_tarball' : 'sync_directory';
$form_state
->setErrorByName($field_name, \Drupal::service('renderer')
->renderPlain($error_message));
}
$install_state['parameters']['profile'] = 'config_installer';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
global $install_state;
$config_storage = new FileStorage(config_get_config_directory(CONFIG_SYNC_DIRECTORY));
$install_state['parameters']['langcode'] = $config_storage
->read('system.site')['langcode'];
}
}