View source
<?php
namespace Drupal\system\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileSystemForm extends ConfigFormBase {
protected $dateFormatter;
protected $streamWrapperManager;
protected $fileSystem;
public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, StreamWrapperManagerInterface $stream_wrapper_manager, FileSystemInterface $file_system) {
parent::__construct($config_factory);
$this->dateFormatter = $date_formatter;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('date.formatter'), $container
->get('stream_wrapper_manager'), $container
->get('file_system'));
}
public function getFormId() {
return 'system_file_system_settings';
}
protected function getEditableConfigNames() {
return [
'system.file',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('system.file');
$form['file_public_path'] = [
'#type' => 'item',
'#title' => $this
->t('Public file system path'),
'#markup' => PublicStream::basePath(),
'#description' => $this
->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'),
];
$form['file_public_base_url'] = [
'#type' => 'item',
'#title' => $this
->t('Public file base URL'),
'#markup' => PublicStream::baseUrl(),
'#description' => $this
->t('The base URL that will be used for public file URLs. This can be changed in settings.php'),
];
$form['file_private_path'] = [
'#type' => 'item',
'#title' => $this
->t('Private file system path'),
'#markup' => PrivateStream::basePath() ? PrivateStream::basePath() : $this
->t('Not set'),
'#description' => $this
->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'),
];
$form['file_temporary_path'] = [
'#type' => 'item',
'#title' => $this
->t('Temporary directory'),
'#markup' => $this->fileSystem
->getTempDirectory(),
'#description' => $this
->t('A local file system path where temporary files will be stored. This directory should not be accessible over the web. This must be changed in settings.php.'),
];
$options = $this->streamWrapperManager
->getDescriptions(StreamWrapperInterface::WRITE_VISIBLE);
if (!empty($options)) {
$form['file_default_scheme'] = [
'#type' => 'radios',
'#title' => $this
->t('Default download method'),
'#default_value' => $config
->get('default_scheme'),
'#options' => $options,
'#description' => $this
->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.'),
];
}
$intervals = [
0,
21600,
43200,
86400,
604800,
2419200,
7776000,
];
$period = array_combine($intervals, array_map([
$this->dateFormatter,
'formatInterval',
], $intervals));
$period[0] = $this
->t('Never');
$form['temporary_maximum_age'] = [
'#type' => 'select',
'#title' => $this
->t('Delete temporary files after'),
'#default_value' => $config
->get('temporary_maximum_age'),
'#options' => $period,
'#description' => $this
->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.'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('system.file')
->set('temporary_maximum_age', $form_state
->getValue('temporary_maximum_age'));
if ($form_state
->hasValue('file_default_scheme')) {
$config
->set('default_scheme', $form_state
->getValue('file_default_scheme'));
}
$config
->save();
parent::submitForm($form, $form_state);
}
}