class MimeDetectSettingsForm in MimeDetect 8
Configure MimeDetect settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\mimedetect\Form\MimeDetectSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of MimeDetectSettingsForm
1 string reference to 'MimeDetectSettingsForm'
File
- src/
Form/ MimeDetectSettingsForm.php, line 12
Namespace
Drupal\mimedetect\FormView source
class MimeDetectSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'mimedetect_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'mimedetect.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('mimedetect.settings');
// Mime detection engines.
$form['engines'] = [
'#type' => 'fieldgroup',
'#title' => $this
->t('MIME detection engines'),
];
// PHP Fileinfo.
$form['engines']['fileinfo_enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('PHP fileinfo'),
'#default_value' => $config
->get('fileinfo.enable'),
'#description' => $this
->t('Use the <a href="@url">PHP file information extension</a>. This is the preferred method.', [
'@url' => 'http://php.net/manual/en/book.fileinfo.php',
]),
];
// UNIX file command.
$form['engines']['unixfile_enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('UNIX file'),
'#default_value' => $config
->get('unixfile.enable'),
'#description' => $this
->t('System call to the file command. Used when PHP fileinfo fails or is not available.'),
];
$form['engines']['unixfile_binary'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path to file command executable'),
'#default_value' => $config
->get('unixfile.binary') ?: '/usr/bin/file',
'#description' => $this
->t("The path to the executable 'file' binary."),
'#states' => [
'visible' => [
':input[name="unixfile_enable"]' => [
'checked' => TRUE,
],
],
],
];
// Default file name extension mapping.
$form['engines']['fileextension_enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('File extension'),
'#default_value' => TRUE,
'#disabled' => TRUE,
'#description' => $this
->t('MIME detection based on filename extension. This is the system default method, used as fall back when all others fail or are not available.'),
];
// Custom MIME 'magic' file.
$form['magicfile'] = [
'#type' => 'textfield',
'#title' => $this
->t("Custom 'magic' file path"),
'#default_value' => $config
->get('magicfile'),
'#description' => $this
->t('Used by any magic based engine. Leave blank to rely on system magic file or PHP internal info.'),
'#states' => [
'enable' => [
[
':input[name="fileinfo_enable"]' => [
'checked' => TRUE,
],
],
[
':input[name="unixfile_enable"]' => [
'checked' => TRUE,
],
],
],
],
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$msg = '';
// Test custom magic file path.
$magic_file = $form_state
->getValue('magicfile');
if (!empty($magic_file) && !MimeDetectService::checkMagicfile($form_state
->getValue('magicfile'), $msg)) {
$form_state
->setErrorByName('magicfile', $msg);
return;
}
// Test fileinfo settings.
if ($form_state
->getValue('fileinfo_enable') && !MimeDetectService::checkFileinfo($magic_file, $msg)) {
if (!empty($magic_file)) {
$form_state
->setErrorByName('magicfile', $msg);
}
else {
$form_state
->setErrorByName('fileinfo_enable', $msg);
}
}
// Test file binary settings.
if ($form_state
->getValue('unixfile_enable') && !MimeDetectService::checkUnixfile($form_state
->getValue('unixfile_binary'), $magic_file, $msg)) {
if (!empty($magic_file)) {
$form_state
->setErrorByName('magicfile', $msg);
}
else {
$form_state
->setErrorByName('unixfile_binary', $msg);
}
}
// Warning about no real MIME detection enable.
if (!$form_state
->getValue('fileinfo_enable') && !$form_state
->getValue('unixfile_enable')) {
$this
->messenger()
->addWarning($this
->t("MimeDetect is using the browser supplied filename for file extension lookups. It is strongly recommended that you install and configure the PHP Fileinfo Extension or the UNIX 'file' command to provide more accurate sever-side mime type detection."));
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save settings changes.
$config = $this
->config('mimedetect.settings');
$config
->set('fileinfo.enable', $form_state
->getValue('fileinfo_enable'));
$config
->set('unixfile.enable', $form_state
->getValue('unixfile_enable'));
$config
->set('unixfile.binary', $form_state
->getValue('unixfile_binary'));
$config
->set('magicfile', $form_state
->getValue('magicfile'));
$config
->save();
parent::submitForm($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
MimeDetectSettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
MimeDetectSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
MimeDetectSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MimeDetectSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
MimeDetectSettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |