b0ca6d129eff360ca7b276f095e6d49cf954372a
[yaffs-website] / web / core / modules / system / src / Form / FileSystemForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Datetime\DateFormatterInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\StreamWrapper\PrivateStream;
9 use Drupal\Core\StreamWrapper\PublicStream;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
12 use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Configure file system settings for this site.
17  *
18  * @internal
19  */
20 class FileSystemForm extends ConfigFormBase {
21
22   /**
23    * The date formatter service.
24    *
25    * @var \Drupal\Core\Datetime\DateFormatterInterface
26    */
27   protected $dateFormatter;
28
29   /**
30    * The stream wrapper manager.
31    *
32    * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
33    */
34   protected $streamWrapperManager;
35
36   /**
37    * Constructs a FileSystemForm object.
38    *
39    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
40    *   The factory for configuration objects.
41    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
42    *   The date formatter service.
43    * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
44    *   The stream wrapper manager.
45    */
46   public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, StreamWrapperManagerInterface $stream_wrapper_manager) {
47     parent::__construct($config_factory);
48     $this->dateFormatter = $date_formatter;
49     $this->streamWrapperManager = $stream_wrapper_manager;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container) {
56     return new static (
57       $container->get('config.factory'),
58       $container->get('date.formatter'),
59       $container->get('stream_wrapper_manager')
60     );
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function getFormId() {
67     return 'system_file_system_settings';
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   protected function getEditableConfigNames() {
74     return ['system.file'];
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function buildForm(array $form, FormStateInterface $form_state) {
81     $config = $this->config('system.file');
82     $form['file_public_path'] = [
83       '#type' => 'item',
84       '#title' => t('Public file system path'),
85       '#markup' => PublicStream::basePath(),
86       '#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web. This must be changed in settings.php'),
87     ];
88
89     $form['file_public_base_url'] = [
90       '#type' => 'item',
91       '#title' => t('Public file base URL'),
92       '#markup' => PublicStream::baseUrl(),
93       '#description' => t('The base URL that will be used for public file URLs. This can be changed in settings.php'),
94     ];
95
96     $form['file_private_path'] = [
97       '#type' => 'item',
98       '#title' => t('Private file system path'),
99       '#markup' => (PrivateStream::basePath() ? PrivateStream::basePath() : t('Not set')),
100       '#description' => t('An existing local file system path for storing private files. It should be writable by Drupal and not accessible over the web. This must be changed in settings.php'),
101     ];
102
103     $form['file_temporary_path'] = [
104       '#type' => 'textfield',
105       '#title' => t('Temporary directory'),
106       '#default_value' => $config->get('path.temporary'),
107       '#maxlength' => 255,
108       '#description' => t('A local file system path where temporary files will be stored. This directory should not be accessible over the web.'),
109       '#after_build' => ['system_check_directory'],
110     ];
111     // Any visible, writeable wrapper can potentially be used for the files
112     // directory, including a remote file system that integrates with a CDN.
113     $options = $this->streamWrapperManager->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE);
114
115     if (!empty($options)) {
116       $form['file_default_scheme'] = [
117         '#type' => 'radios',
118         '#title' => t('Default download method'),
119         '#default_value' => $config->get('default_scheme'),
120         '#options' => $options,
121         '#description' => t('This setting is used as the preferred download method. The use of public files is more efficient, but does not provide any access control.'),
122       ];
123     }
124
125     $intervals = [0, 21600, 43200, 86400, 604800, 2419200, 7776000];
126     $period = array_combine($intervals, array_map([$this->dateFormatter, 'formatInterval'], $intervals));
127     $period[0] = t('Never');
128     $form['temporary_maximum_age'] = [
129       '#type' => 'select',
130       '#title' => t('Delete temporary files after'),
131       '#default_value' => $config->get('temporary_maximum_age'),
132       '#options' => $period,
133       '#description' => t('Temporary files are not referenced, but are in the file system and therefore may show up in administrative lists. <strong>Warning:</strong> If enabled, temporary files will be permanently deleted and may not be recoverable.'),
134     ];
135
136     return parent::buildForm($form, $form_state);
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   public function submitForm(array &$form, FormStateInterface $form_state) {
143     $config = $this->config('system.file')
144       ->set('path.temporary', $form_state->getValue('file_temporary_path'))
145       ->set('temporary_maximum_age', $form_state->getValue('temporary_maximum_age'));
146
147     if ($form_state->hasValue('file_default_scheme')) {
148       $config->set('default_scheme', $form_state->getValue('file_default_scheme'));
149     }
150     $config->save();
151
152     parent::submitForm($form, $form_state);
153   }
154
155 }