Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 45 |
ConfigImportForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 45 |
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
create | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
getFormId | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
buildForm | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 11 |
|||
validateForm | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 8 |
|||
submitForm | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 18 |
<?php | |
/** | |
* @file | |
* Contains \Drupal\config\Form\ConfigImportForm. | |
*/ | |
namespace Drupal\config\Form; | |
use Drupal\Core\Archiver\ArchiveTar; | |
use Drupal\Core\Config\StorageInterface; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Defines the configuration import form. | |
*/ | |
class ConfigImportForm extends FormBase { | |
/** | |
* The configuration storage. | |
* | |
* @var \Drupal\Core\Config\StorageInterface | |
*/ | |
protected $configStorage; | |
/** | |
* Constructs a new ConfigImportForm. | |
* | |
* @param \Drupal\Core\Config\StorageInterface $config_storage | |
* The configuration storage. | |
*/ | |
public function __construct(StorageInterface $config_storage) { | |
$this->configStorage = $config_storage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('config.storage.sync') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return 'config_import_form'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$form['import_tarball'] = array( | |
'#type' => 'file', | |
'#title' => $this->t('Configuration archive'), | |
'#description' => $this->t('Allowed types: @extensions.', array('@extensions' => 'tar.gz tgz tar.bz2')), | |
); | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => $this->t('Upload'), | |
); | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state) { | |
$file_upload = $this->getRequest()->files->get('files[import_tarball]', NULL, TRUE); | |
if ($file_upload && $file_upload->isValid()) { | |
$form_state->setValue('import_tarball', $file_upload->getRealPath()); | |
} | |
else { | |
$form_state->setErrorByName('import_tarball', $this->t('The file could not be uploaded.')); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
if ($path = $form_state->getValue('import_tarball')) { | |
$this->configStorage->deleteAll(); | |
try { | |
$archiver = new ArchiveTar($path, 'gz'); | |
$files = array(); | |
foreach ($archiver->listContent() as $file) { | |
$files[] = $file['filename']; | |
} | |
$archiver->extractList($files, config_get_config_directory(CONFIG_SYNC_DIRECTORY)); | |
drupal_set_message($this->t('Your configuration files were successfully uploaded and are ready for import.')); | |
$form_state->setRedirect('config.sync'); | |
} | |
catch (\Exception $e) { | |
drupal_set_message($this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', array('@message' => $e->getMessage())), 'error'); | |
} | |
drupal_unlink($path); | |
} | |
} | |
} | |