FileHashConfigForm.php in File Hash 8
File
src/Form/FileHashConfigForm.php
View source
<?php
namespace Drupal\filehash\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileHashConfigForm extends ConfigFormBase {
protected $moduleHandler;
public function getFormId() {
return 'filehash_config_form';
}
protected function getEditableConfigNames() {
return [
'filehash.settings',
];
}
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['algos'] = [
'#default_value' => $this
->config('filehash.settings')
->get('algos'),
'#description' => $this
->t('The checked hash algorithm(s) will be calculated when a file is saved. For optimum performance, only enable the hash algorithm(s) you need.'),
'#options' => filehash_names(),
'#title' => $this
->t('Enabled hash algorithms'),
'#type' => 'checkboxes',
];
$form['dedupe'] = [
'#default_value' => $this
->config('filehash.settings')
->get('dedupe'),
'#description' => $this
->t('If checked, prevent duplicate uploaded files from being saved. Note, enabling this setting has privacy implications, as it allows users to determine if a particular file has been uploaded to the site.'),
'#title' => $this
->t('Disallow duplicate files'),
'#type' => 'checkbox',
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$original_algos = $this
->config('filehash.settings')
->get('algos');
$this
->config('filehash.settings')
->set('algos', $form_state
->getValue('algos'))
->set('dedupe', $form_state
->getValue('dedupe'))
->save();
if ($this->moduleHandler
->moduleExists('views') && $form_state
->getValue('algos') != $original_algos) {
views_invalidate_cache();
}
parent::submitForm($form, $form_state);
}
}