0c3def8619d6ef814979da2dbb693555e3768373
[yaffs-website] / web / core / modules / config / src / Form / ConfigImportForm.php
1 <?php
2
3 namespace Drupal\config\Form;
4
5 use Drupal\Core\Archiver\ArchiveTar;
6 use Drupal\Core\Config\StorageInterface;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines the configuration import form.
13  */
14 class ConfigImportForm extends FormBase {
15
16   /**
17    * The configuration storage.
18    *
19    * @var \Drupal\Core\Config\StorageInterface
20    */
21   protected $configStorage;
22
23   /**
24    * Constructs a new ConfigImportForm.
25    *
26    * @param \Drupal\Core\Config\StorageInterface $config_storage
27    *   The configuration storage.
28    */
29   public function __construct(StorageInterface $config_storage) {
30     $this->configStorage = $config_storage;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('config.storage.sync')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return 'config_import_form';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function buildForm(array $form, FormStateInterface $form_state) {
53     $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
54     $directory_is_writable = is_writable($directory);
55     if (!$directory_is_writable) {
56       drupal_set_message($this->t('The directory %directory is not writable.', ['%directory' => $directory]), 'error');
57     }
58     $form['import_tarball'] = [
59       '#type' => 'file',
60       '#title' => $this->t('Configuration archive'),
61       '#description' => $this->t('Allowed types: @extensions.', ['@extensions' => 'tar.gz tgz tar.bz2']),
62     ];
63
64     $form['submit'] = [
65       '#type' => 'submit',
66       '#value' => $this->t('Upload'),
67       '#disabled' => !$directory_is_writable,
68     ];
69     return $form;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function validateForm(array &$form, FormStateInterface $form_state) {
76     $all_files = $this->getRequest()->files->get('files', []);
77     if (!empty($all_files['import_tarball'])) {
78       $file_upload = $all_files['import_tarball'];
79       if ($file_upload->isValid()) {
80         $form_state->setValue('import_tarball', $file_upload->getRealPath());
81         return;
82       }
83     }
84
85     $form_state->setErrorByName('import_tarball', $this->t('The file could not be uploaded.'));
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function submitForm(array &$form, FormStateInterface $form_state) {
92     if ($path = $form_state->getValue('import_tarball')) {
93       $this->configStorage->deleteAll();
94       try {
95         $archiver = new ArchiveTar($path, 'gz');
96         $files = [];
97         foreach ($archiver->listContent() as $file) {
98           $files[] = $file['filename'];
99         }
100         $archiver->extractList($files, config_get_config_directory(CONFIG_SYNC_DIRECTORY));
101         drupal_set_message($this->t('Your configuration files were successfully uploaded and are ready for import.'));
102         $form_state->setRedirect('config.sync');
103       }
104       catch (\Exception $e) {
105         drupal_set_message($this->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', ['@message' => $e->getMessage()]), 'error');
106       }
107       drupal_unlink($path);
108     }
109   }
110
111 }