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