3 namespace Drupal\system\Form;
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;
16 * Configure file system settings for this site.
18 class FileSystemForm extends ConfigFormBase {
21 * The date formatter service.
23 * @var \Drupal\Core\Datetime\DateFormatterInterface
25 protected $dateFormatter;
28 * The stream wrapper manager.
30 * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
32 protected $streamWrapperManager;
35 * Constructs a FileSystemForm object.
37 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38 * The factory for configuration objects.
39 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
40 * The date formatter service.
41 * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
42 * The stream wrapper manager.
44 public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, StreamWrapperManagerInterface $stream_wrapper_manager) {
45 parent::__construct($config_factory);
46 $this->dateFormatter = $date_formatter;
47 $this->streamWrapperManager = $stream_wrapper_manager;
53 public static function create(ContainerInterface $container) {
55 $container->get('config.factory'),
56 $container->get('date.formatter'),
57 $container->get('stream_wrapper_manager')
64 public function getFormId() {
65 return 'system_file_system_settings';
71 protected function getEditableConfigNames() {
72 return ['system.file'];
78 public function buildForm(array $form, FormStateInterface $form_state) {
79 $config = $this->config('system.file');
80 $form['file_public_path'] = [
82 '#title' => t('Public file system path'),
83 '#markup' => PublicStream::basePath(),
84 '#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 $form['file_public_base_url'] = [
89 '#title' => t('Public file base URL'),
90 '#markup' => PublicStream::baseUrl(),
91 '#description' => t('The base URL that will be used for public file URLs. This can be changed in settings.php'),
94 $form['file_private_path'] = [
96 '#title' => t('Private file system path'),
97 '#markup' => (PrivateStream::basePath() ? PrivateStream::basePath() : t('Not set')),
98 '#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 $form['file_temporary_path'] = [
102 '#type' => 'textfield',
103 '#title' => t('Temporary directory'),
104 '#default_value' => $config->get('path.temporary'),
106 '#description' => t('A local file system path where temporary files will be stored. This directory should not be accessible over the web.'),
107 '#after_build' => ['system_check_directory'],
109 // Any visible, writeable wrapper can potentially be used for the files
110 // directory, including a remote file system that integrates with a CDN.
111 $options = $this->streamWrapperManager->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE);
113 if (!empty($options)) {
114 $form['file_default_scheme'] = [
116 '#title' => t('Default download method'),
117 '#default_value' => $config->get('default_scheme'),
118 '#options' => $options,
119 '#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.'),
123 $intervals = [0, 21600, 43200, 86400, 604800, 2419200, 7776000];
124 $period = array_combine($intervals, array_map([$this->dateFormatter, 'formatInterval'], $intervals));
125 $period[0] = t('Never');
126 $form['temporary_maximum_age'] = [
128 '#title' => t('Delete orphaned files after'),
129 '#default_value' => $config->get('temporary_maximum_age'),
130 '#options' => $period,
131 '#description' => t('Orphaned files are not referenced from any content but remain in the file system and may appear in administrative listings. <strong>Warning:</strong> If enabled, orphaned files will be permanently deleted and may not be recoverable.'),
134 return parent::buildForm($form, $form_state);
140 public function submitForm(array &$form, FormStateInterface $form_state) {
141 $config = $this->config('system.file')
142 ->set('path.temporary', $form_state->getValue('file_temporary_path'))
143 ->set('temporary_maximum_age', $form_state->getValue('temporary_maximum_age'));
145 if ($form_state->hasValue('file_default_scheme')) {
146 $config->set('default_scheme', $form_state->getValue('file_default_scheme'));
150 parent::submitForm($form, $form_state);