RemovalSettingsForm.php in General Data Protection Regulation 3.0.x
File
modules/gdpr_tasks/src/Form/RemovalSettingsForm.php
View source
<?php
namespace Drupal\gdpr_tasks\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RemovalSettingsForm extends ConfigFormBase {
protected $fileSystem;
const CONFIG_KEY = 'gdpr_tasks.settings';
const EXPORT_DIRECTORY = 'export_directory';
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('file_system'));
}
public function __construct(ConfigFactoryInterface $configFactory, FileSystemInterface $fileSystem) {
parent::__construct($configFactory);
$this->fileSystem = $fileSystem;
}
protected function getEditableConfigNames() {
return [
self::CONFIG_KEY,
];
}
public function getFormId() {
return 'gdpr_tasks_removal_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('Export Directory'),
'#description' => $this
->t('Specifies the path to the directory where Right to be Forgotten tasks are exported after being processed'),
'#required' => TRUE,
'#default_value' => $this
->config(self::CONFIG_KEY)
->get(self::EXPORT_DIRECTORY),
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($form_state
->hasValue('directory')) {
$directory = $form_state
->getValue('directory');
if (empty($directory)) {
$form_state
->setErrorByName('directory', $this
->t('The directory is required.'));
return;
}
if (!$this->fileSystem
->prepareDirectory($directory)) {
$form_state
->setErrorByName('directory', $this
->t("The directory does not exist or it's not writable."));
return;
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config(self::CONFIG_KEY)
->set(self::EXPORT_DIRECTORY, $form_state
->getValue('directory'))
->save();
$this
->messenger()
->addStatus('Changes saved.');
}
}